-
A local variable (an internal variable) is defined inside a function, and its scope is limited to the variable inside the function that defines it.
Global variables (external variables) are defined outside of the function, and the scope is the entire program.
-
As follows:
1. Different scopes: The scope of global variables is the whole program, while the scope of local variables is the current function or loop.
2. The memory storage mode is different: global variables are stored in the global data area, and local variables are stored in the stack area.
3. Different lifetimes: The lifetimes of global variables are the same as those of the main program, which are destroyed with the destruction of the program, and the local variables are inside the function or loop, and they do not exist with the exit of the function or the exit of the loop.
4. Different ways of use: global variables can be used in all parts of the program after declaration, but local variables can only be used locally. It is important to note that local variables cannot be assigned to the value of a global variable with the same name.
-
The difference between them lies in the scope of the variables. Generally speaking, the influence range of global variables is greater than that of local variables, and local variables generally only act on a block-level scope, such as a loop body, a function body, and so on.
-
Global variables: The scope of action is "the entire project", which can be defined in one source file and can be applied to all source files. Of course, other source files that do not contain a global variable definition need to be declared again with the extern keyword, which is large in scope.
Local variables: only from the defined position to the end of the closing curly brace that defines it, only exist during the execution of the function, after the execution of a call to the function, the variable is revoked, and the memory occupied by it is also retracted, and the scope is small.
-
First, the use is different:
Global variables: Describe what the object has (defined in the class), which can be used by all methods in the class.
Local variables: Temporarily holds data (defined in the class's method) and can only be used in the current method.
Second, the use is different.
Global variable = available in the entire class.
Local variable = available within the method.
Third, the initial value is different.
Global variable = has an initial value.
Local variable = no initial value.
-
a=1 b=2 are global variables, that's true.
Because in the main function, fun2(); In the function fun2, if the global variable is assigned a new value, a=4 and b=9, then the global variable ab becomes the new value and outputs its new value.
The meaning of a global variable is simply that all functions can use it or can be used globally, not that its value is immutable, as you understand. Unless you decorate this variable with const, i.e. the global variable const int a=1; , this a is immutable, if you change the value of a in the function, the compilation will not pass.
-
Global variables:
Variables defined outside of all functions are called global variables, and their scope defaults to the entire program, i.e., all source files, including .c and .h file.
In short, global variables can be used, but care should be taken to make their names as easy to understand as possible, and not too short to avoid contamination of namespaces; Avoid using global variables for huge objects.
Local variables:
In a program, variables that are accessible only in a particular procedure or function are relative to global variables.
Global variables, also known as external variables, are defined outside of the function and are scoped from the point where the variable is defined to the end of the program file. All global variables are stored in static storage, and a storage area is allocated to the global variables when the program starts to execute, and they are released when the program is finished.
Local variables can have the same name as global variables, but local variables block global variables. When referencing this variable in a function, a local variable with the same name is used, not a global variable. It is differentiated according to the scope of use.
Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More
Define the struct:
typedef struct _legaladdress_{ >>>More
Just take a look at this one**.
Hope it helps. >>>More
It's called the [batch] program, in fact, don't think that the program you mentioned above to deal with garbage is really powerful, it's amazing, when you have learned DOS, that program can be said to be a very simple thing; >>>More
Scope. You static char *chh;
static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More