Definitions and calls of global variables in C Language

Source: Internet
Author: User

1. Definitions of 'global variable' in Tan haoqiang's book

Variables defined in a function are local variables, while variables defined outside a function are external variables, and external variables are global variables. Global variables can be shared by other functions in this file. Their effective ranges start from the location of the defined variable to the end of the source file.

The role of setting up global variables is to increase the channels for data connection between functions.

2. Statement of 'global variable' in Tan haoqiang's book

Declare with extern:

If an external variable is not defined at the beginning of the file, its effective scope can only be from the definition point to the end of the file. If the function before the definition point wants to reference this external variable, you should use the keyword extern to declare this variable before the application '. This variable is an external variable that has been defined. With this declaration, you can legally call this external variable from the Declaration.

Int A = 13, B =-8;

 

....

 

 

Main ()

{

Extern int A, B; // You can also write it as extern A, B;

....

}

 

Use static declaration:

Sometimes, in a program, you want some external variables to be referenced only by this file, but not by other files. In this case, you can add a static declaration before external variables. At this time, the external variables also become static global variables.

Static int;

 

3. Notes

Definitions and declarations of external variables are different. An external variable can only be defined once, and its position is outside all functions. The declaration of an external variable in a unified file can be multiple times, and its position can be within the function, it can also be outside the function. The system allocates storage units according to the definition of external variables rather than declarations. Initialization of external variables can only be performed during definition, but not in Declaration.

 

 

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.