C + + Learning Notes (v) various variables && extern, static keyword __c++

Source: Internet
Author: User

1, local variables: Only local variables, such as in the function body, in the For loop, in the IF statement to declare the defined variables, if not a specific keyword, he is a local variable.

2. Global variables: Variables that act on the entire program can be shared by multiple source code files. It is defined outside the function.

Using a defined global variable in a different source program requires an extern keyword declaration to tell the compiler that this is a global variable that has already been defined in another source code. Global variables can only be defined once, but may be declared multiple times.

File_1.cpp
int counter;//defines counter, global variable
//file_2.cpp
extern int counter;//declares global variables to use File_1. Global variable counter++ in CPP
;

3. Local Variables Mask global variables

extern int counter;
int global = ten;
int _tmain (int argc, _tchar* argv[])
{
	counter--;
	cout << counter << Endl;
	int global = 2;//local variable and global variable with the same name, global variable is masked
	cout << global << endl;//output is 2
	cout <<:: Global << ENDL;//uses:: Invokes the masked global variable, the output is
	0;

4. Static global variables and functions

Add the static keyword before global variables and functions so that global variables and functions are only active in the current source file, hidden from other files, so that you can define a function with the same name and a variable with the same name in different files without worrying about naming conflicts.

For example, I defined an int global in a.cpp and B.cpp, and the runtime would have an error: Error LNK2005: "int global" has been defined in A.cpp, fatal error LNK1169: Find one or more multiple-defined symbols. The workaround is to hide the global variable by adding the static keyword in front of int global in one of the CPP.

function in the same vein

5. Static local Variables

Defined within a function, but does not end the lifecycle with the Exit function, its lifecycle is the entire source program, but the scope is limited to this function only. After exiting the function, the variable is still available, and the variable can be changed when the function is called again. You can use this variable only within the function that defines the variable.

The Void Add ()
{
	static int count = 0;//count is stored in the initialized data segment, and the global initialization area
	count++;
	cout << "static local variable" <<count << Endl;
}
///static is used as a static local variable, preserving the persistent
///
Add () of the local variable content;
Add ()//Call two times function add to implement the sum of the static local variable count in the function.

The output is first called 1, and the second call is 2

6. Static as the default initialization variable

///static is used as the default initialization
///
int array1[1000];
cout << "uninitiated variable" << array1[999] << Endl;
static int array2[1000];
cout << "Default initialization" << array2[999] << Endl;

The output of the first statement is uninitiated variable-858993460

The second statement outputs the result: Default initialization 0

Visible before the variable with static, will initialize the variable default, reduce the workload, not one to initialize to 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.