Global variables in C/C ++

Source: Internet
Author: User

Variables in C/C ++ include global variables, static global variables, local variables, and static local variables, in "Characteristics and Application of Static local variables in C/C ++", we have introduced static local variables. Today, our goal is global variables.

Global variables in a single file

To put it simply, global variables are defined outside the function. The following is a simple example. Two int-type global variables A and B are defined in total, the positions of the two global variables are not the same. A is defined before fun1, B is defined, and A and B are defined before Main, from the usage perspective, global variable A can be used in all functions behind it, while global variable B can only be used in function main, rather than fun1 before its definition. Therefore, we can conclude that the scope of a global variable starts from its defined position and ends with the end of the file, that is, onlyFile Scope.

C ++ language: Zhiyi blog

# Include "stdio. h"

Int A = 1; // This is a global variable.

Void fun1 () // This function can only use the global variable A, not B
{
A ++;
Printf ("A = % d \ n", );
}

Int B = 2; // This is also a global variable.

Void main () // here a B can be used
{
Fun1 ();
A ++;
Printf ("A = % d, B = % d \ n", a, B );
Getchar ();
}

Make global variables valid in multiple files

As mentioned above, the scope of a global variable starts from the defined place until the end of the file. Isn't that usable in other files. From the perspective of the degree of Nb in c/C ++, this is impossible. To extend the scope of global variables to multiple files, the extern keyword is provided in C ++.

In the example code below, the extern int g_c is used as a declaration to say that g_c is a global variable. You can use it at will, g_c itself is defined in other files (this file can also be defined, but it must be ensured that it is not defined in other files ), in this way, the scope of global variables is extended to multiple files.

Global variables can be used in other source files as long as they are defined in a file and the extern keyword is used:Extern extends the scope of global variables

C ++ language: Zhiyi blog

# Include "stdio. h"

Extern int g_c; // declare that this is an external global variable

Int G_A = 1; // global variable

Void fun1 ()
{
G_A ++;
Printf ("G_A = % d, g_c = % d \ n", G_A, g_c );
}

Int g_ B = 2; // global variable

Void main ()
{
Fun1 ();
G_A ++;
Printf ("G_A = % d, g_ B = % d, g_c = % d \ n", G_A, g_ B, g_c); // use g_c directly here.
Getchar ();
}

A small change was made to add all global variables with the G _ prefix to indicate that this is a global variable.

Restrict global variables to be valid only in one file

It was hard to extend the scope of global variables to multiple files. Why do I have to limit it now? This is because, although the extern keyword is not used after global variables are defined, they can only be used in this file. However, if you define global variables with the same name in other files, it is not allowed to drop (errors will occur during the link). We will discuss the cause later.

To enable the use of global variables with the same name in different files, the static keyword comes in handy, we have already mentioned the static keyword in the characteristics and applications of static local variables in C/C ++. Local variables modified with static variables can only be used in functions defining variables, so the global variables modified with static can only be used in the file defining them. This is the legendary static global variable.

C ++ language: Zhiyi blog # include "stdio. h"

Extern int g_c; // global variables defined in other files

Static int G_A = 1; // static global variables are used in this file.

Void fun1 ()
{
G_A ++;
Printf ("G_A = % d, g_c = % d \ n", G_A, g_c );
}

Int g_ B = 2; // global variable

Void main ()
{
Fun1 ();
G_A ++;
Printf ("G_A = % d, g_ B = % d, g_c = % d \ n", G_A, g_ B, g_c );
Getchar ();
}

After static modification, we can use global variables with the same name in different files without Link errors.

 

Download sample program source code

 


Original article please indicate reprinted in Zhiyi blog, this article address: http://www.letuknowit.com/archives/87

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.