C Scope rules

In any kind of programming, scope is the area in which variables defined in a program exist; beyond that area, the variables cannot be accessed. In the C language, there are three places where variables can be declared:

  1. Inside a function or blockLocalVariable
  2. Outside all functionsglobalVariable
  3. InformIn the function parameter definitions

Let's take a look at whatLocalvariables,globalvariables andformparameters.

Local variables

Variables declared inside a function or block are called local variables. They can only be used by statements inside that function or code block. Local variables are not known outside the function. Here is an example of using local variables. Here, all variables a, b, and c are local variables of the main() function.

Example

#include <stdio.h> int main () { /*Local variable declaration*/ int a, b; int c; /*actual initialization*/ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0; }

Global variables

Global variables are defined outside functions, usually at the top of the program. Global variables remain valid throughout the entire program lifecycle, and can be accessed inside any function.

Global variables can be accessed by any function. That is, after a global variable is declared, it is available throughout the entire program. The following is an example of using global and local variables:

Example

#include <stdio.h> /*Global variable declaration*/ int g; int main () { /*Local variable declaration*/ int a, b; /*actual initialization*/ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0; }

In a program, local variables and global variables can have the same name, but inside a function, if two names are the same, the local variable value is used, and the global variable is not used. Here is an example:

In a program, local variables and global variables

Example

#include <stdio.h> /*Global variable declaration*/ int g = 20; int main () { /*Local variable declaration*/ int g = 10; printf ("value of g = %d\n", g); return 0; }

When the above code is compiled and executed, it produces the following results:

value of g = 10

Formal parameters

Function parameters, i.e., formal parameters, are treated as local variables within that function, and if they have the same name as global variables, they take precedence. Here is an example:

Example

#include <stdio.h> /*Global variable declaration*/ int a = 20; int main () { /*Local variable declarations in the main function*/ int a = 10; int b = 20; int c = 0; int sum(int, int); printf ("value of a in main() = %d\n", a); c = sum( a, b); printf ("value of c in main() = %d\n", c); return 0; } /*Function to add two integers*/ int sum(int a, int b) { printf ("value of a in sum() = %d\n", a); printf ("value of b in sum() = %d\n", b); return a + b; }

When the above code is compiled and executed, it produces the following results:

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

The difference between global variables and local variables in memory:

  • Global variables are stored in the global storage area of memory, occupying static storage units;
  • Local variables are stored in the stack, and only when the function in which they are located is called are storage units dynamically allocated for the variables.

For more information, see:Usage of static in C/C++: global variables and local variables

Initializing local and global variables

When a local variable is defined, the system does not initialize it; you must initialize it yourself. When defining a global variable, the system automatically initializes it, as shown below:

Data typesInitialize default values
int 0
char '\0'
float 0
double 0
pointer NULL

Correctly initializing variables is a good programming practice; otherwise, the program may sometimes produce unexpected results, because uninitialized variables can cause garbage values already available in memory locations.

other extensions