The static function of C language __c language

Source: Internet
Author: User

In C, the literal meaning of static can easily lead us astray, in fact, there are three functions:

(1) The first and most important one: hide

When our colleagues compile multiple files, all global variables and functions without a static prefix have global visibility, for example: Compile 2 source files at the same time: A.C and MAIN.C

The following is the contents of a.c char a = ' a '; Global variable
int msg ()
{
printf ("hello\n"); return 0;
}

The following is the contents of main.c int main (void)
{
extern char A; extern variable must is declared before use extern int msg ();
printf ("%c", a);
MSG ();
return 0;
}

The results of the program's operation are:

A Hello

This result is not difficult to understand because of the use of declaration extern.

But if you add static to a, you hide it, MAIN.C can't see A and MSG functions anymore, so the question is, why use static, in fact, using this feature, you can define the same name in different files and variables with the same name, only need to add static, this is not more than worry about naming conflicts, in general, the role of static is limited to hide.

(2) The second function of static is to keep the variable content persistent, variables stored in the static data area are initialized at the start of the program, which is also the only initialization, with 2 variables stored in the static store: Global and static variables, but compared to global variables, Static can control the visible range of variables, in the end static or to hide. Examples are as follows:

#include <stdio.h>

int fun (void) {
static int count = 10; In fact, this assignment statement has never been executed
return count--;
}

int count = 1;

int main (void)
{
printf ("Global\t\tlocal static\n");
for (; Count <= ++count)
printf ("%d\t\t%d\n", Count, Fun ());

return 0;
}

Program Run Result:


With this running result, you can find that the static is still used to hide.

(3) The third role of Static is to initialize the default to 0, in fact, the global variable also has this attribute, because global variables are also stored in the static data area, in the static data area, all the bytes in memory default values are 0x00, this feature can reduce the workload of programmers.


Finally, the role of Static is summed up: first the main function of static is to hide, and second, because the static variable is stored in the quiescent storage area, it has a persistence and default value of 0.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.