Variables are very common in advanced languages, and the C language is the case, so the in-depth understanding of variables have great benefits to preparing high quality code! Since the natural blood relationship between C and C languages, the principles and examples herein are equally applicable to C languages. Now, we will talk about the variables from the following aspects:
First, the scope of the variable
The scope of the variable, in other words, the scope of the variable. Variables are scope, not after you define the variable, you can use it anywhere in the program. E.g:
Int a;
Void F1 ()
{
INT B;
B = a; / / correct
}
Void F2 ()
{
a = 10; // correct
B = 10; // Error
}
In the above code, the variables A, B have their own scope, and the scope of A is globally, and it can be used anywhere after A definition, we call global variables; B's scope is Partially referred to as partial variables, only within the function F1, so that the value is incorrect to the variable B in the function F2. Usually we can think that the scope of the variable is bound to {}, for example:
Void F3 ()
{
INT C;
c = 10; // correct
{
INT D;
D = C; / / correct
}
D = 1; // Error
}
The variable c is defined in the function F3, so it is valid in the entire function range of F3, and the variable D is defined in a pair of {} of the function F3, and its definition domain can only be here. In {}, when the variable D is used after {}, it is illegal. Compiled in the VC, prompts "Error C2065: 'D': undeclared Identifier" error, means that the identifier D has no declaration.
In addition, when the global variable and the local variable are renamed, when the scope of the two variables overlap, the scope of the local variable is subject to, for example:
INT A = 0;
void main ()
{
INT A = 1;
{
INT A = 2;
Printf ("A =% D / N", A);
}
Printf ("A =% D / N", A);
}
The output result of the program is:
A = 2
A = 1
Second, global variables and local variables
The global variable and local variable have just been mentioned, but the variable defined outside {} is a global variable? We look at the following procedures:
// file1.c
#include
Void F5 (int);
Static int E;
Void F4 (INT A)
{
e = a;
}
void main ()
{
F5 (10);
}
// file2.c
Void F4 (int);
Int E;
Void F5 (INT B)
{
e = 5;
F4 (b);
Printf ("E =% D / N", E);
}
First, in the main function, the parameter call function f5 is called, in the function f5, the value of the variable E is 5, next to the function f4, the value of the parameter B is 10, and the value of the variable E is assigned to the parameter A in the function F4. Value 10, Note: This e is non-He. Let's first look at the definition of the variable e in two files. The only difference is that there are more keywords in filefile1.c: static. However, this Static makes the variable E in file1.c become local variables, in other words, this variable E is not globally, its scope value is valid in file1.c file, in File2. It doesn't work in c. So it is not difficult to see that the result of this program is: E = 5
Of course, if you want to make the variable E action domain, the program can be modified as follows:
// file1.c
#include
Void F5 (int);
Int E;
Void F4 (INT A)
{
e = a;
}
void main ()
{
F5 (10);
}
// file2.c
Void F4 (int);
Extern Int E;
Void F5 (INT B)
{
e = 5;
F4 (b);
Printf ("E =% D / N", E);
}
File file2.c has more keywords: extern, indicating that this variable is external, and
Extern Int E;
Not a definition of variables, it is just a declaration of a variable, definition, and declaration. We will introduce below. The result of the program after modification is:
E = 10
In actual work, we should use global variables as little as possible, especially in projects that have co-coded, each person is responsible for the writing of a part of code, and it is possible to cause a conflict of global variables. When to be in a file When using multiple functions in this file to use the variables to be used in front of the variable: Static, the variable is enabled only in this document, avoiding the variable name conflict with others.
Third, the definition and statement of variables
Under normal circumstances, the definition and statement of variables in C procedures is one of them, such as:
Int a;
We generally call the definition of variables, this is not as obvious. When we do want to declare a variable, we use the following statement:
Extern Int A;
Keyword extern indicates that the variable is external, in other words, here is only the existence of this variable, so that the variable can be used in other files, or within this document, such as:
#include
Extern Int A;
void main ()
{
Printf ("A =% D / N", A);
}
INT A = 5;
In this code, if there is no upper extern int a; this sentence is a declaration of the variable A, it is illegal, because the compiler is not found in compilation; If there is no such int a = 5; although the program can be compiled, it will also appear when the connection (LINK) is connected, and the prompt does not find the definition of the variable A.
The definition of variables and the most important difference is to define variables to allocate storage space for variables, and declare that a variable does not need. Therefore, Extern Int A; this statement does not assign memory space for variable a, and if there is no definition of variable A, then any operation of variable A is illegal, because this variable does not exist. The definition of variables and declarations that another difference is that defining variables can be assigned to variables, such as:
INT A = 5;
And the statement is not available, such as:
EXTERN INT A = 5; // Error
This error is obvious, because this 5 does not store space stored.
Fourth, the storage space of the variable
Simply put, in the variable defined in the function, in the stack (STACK) unless you define the variables as static type, when he and other variables defined outside the function, all assign in global memory. in. E.g:
#include
INT A, B;
Static Int C;
Main ()
{
Static int D;
Int E;
a = 1;
B = 2;
C = 3;
D = 4;
e = 5;
}
In the above program, both variables A, B, C, D are stored in the global memory, and the variable E is stored in the stack. Since the function performs the end, the restore stack is performed, so the storage space of the variable defined inside the function is not retained after the function returns.
Therefore, when we see the value of the value of the variable needs to be retained, we generally define the variables as a Static type. At this time, the variable is stored in the global memory. When the function is reduced, the value of the variable still exists, It is this reason.
Fifth, the initialization of variables
The initialization of variables is quite familiar with everyone, and the form is relatively simple:
INT A = 5;
This is the simplest initialization statement, if it is the following statement:
Int a;
So what is the default initial value of the variable A? The answer is uncertain. Of course, it is not said that the default value of A is arbitrary, but the location of the variable variable A defined, the operating system, and the compiler used. For example, using a VC 6.0 compiler on the Windows platform, if int A; appears outside all functions, then the default initial value is 0, but if int A; appears inside the function, then in general A The default initial value is 0xcc, everyone will be surprising, why is Microsoft use such a strange initial value? I think because A is assigned in the stack, the World War I should not be damaged. If there is an illegal statement inexplicably jumped to this, 0xcc is the binary code in the assembly statement (INT 3), so the program It will be interrupted to prevent unpredictable results. But on the Linux platform, the default initial value of the variable is generally 0.
Therefore, when we write the program, we must develop a good habit of adding an initial value to the variable.
Let's talk about the initialization of the array.
Char a [100];
Char a [100] = {'a'};
These two statements are defined a character array, and the second statement is initialized to the array. Let's discuss a meaningful topic, what is the impact of these two statements have an impact on the size of the executable after compiling connectivity?
Of course, if the initialization of the array is in the function, it is not obvious, but the outside of all functions is defined, it is not the same, do not believe you can try the following:
#include
void main ()
{
}
When you compile, you must first confirm your disk remaining space. If less than 100m, please do not test, otherwise the consequences are at your own risk! Speaking here, you should understand, the executable after compiling the connection is more than 100 M, scary! In fact, this is determined by the format of executable file on the Windows platform, interested friends can study the structure of Windows PE files.
We discussed above
C
Some small problems about variables in the language, but I have encountered in actual work and learning, of course, there is no more mistakes, I hope you can tell me, I will be very happy.