Scope rules in programming C

A scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed.

A scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed. There are 3 places where variables can be declared in Language C:

  1. Inside a function or a code block is called a local variable.
  2. Outside all functions and called global variables .
  3. In the definition of functions, parameters are called formal - formal parameters.

Let us now find out what local variables and global variables and formal parameters are.

Local variable in C

Local variables are declared inside a function or declared inside a code block. They are used by commands in that function or code block. Local variables are not used outside of the function. Below is an example of using local variables. Here variables a, b and c are used in main ():

 #include int main () { /* Khai bao bien cuc bo */ int a , b ; int c ; /* khoi tao thuc su */ a = 10 ; b = 20 ; c = a + b ; printf ( "Gia tri cua a = %d, b = %d va c = %dn" , a , b , c ); return 0 ; } 

Global variable in C

Global variables are defined outside a function, usually the first part of the program. Global variables can contain values ​​during program run and can be accessed by any function defined in the program.

A global variable can be accessed by any function. That means global variables are used throughout the program after it declares. Here is an example to illustrate local and global variables:

 #include /* Khai bao bien toan cuc */ int g ; int main () { /* Khai bao bien cuc bo */ int a , b ; /* khoi tao bien thuc su */ a = 10 ; b = 20 ; g = a + b ; printf ( "Gia tri cua a = %d, b = %d va g = %dn" , a , b , g ); return 0 ; } 

A program can have global and local variables with the same name. In that case, the local variable inside the function will be used. Here is an example:

 #include /* Khai bao bien toan cuc */ int g = 20 ; int main () { /* khai bao bien cuc bo */ int g = 10 ; printf ( "Gia tri cua g = %dn" , g ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Run the above C program to compile and run to see the results:

Official parameter in C

The function parameter, the official parameter, is treated as a local variable within that function and usually takes precedence over the global variable. Here is an example:

 #include /* khai bao bien toan cuc */ int a = 20 ; /* khai bao ham */ int hamtinhtong ( int a , int b ); int main () { /* khai bao bien cuc bo trong ham main */ int a = 15 ; int b = 25 ; int c = 0 ; printf ( "Gia tri cua a trong ham main() = %dn" , a ); c = hamtinhtong ( a , b ); printf ( "Gia tri cua c trong ham main() = %dn" , c ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } /* ham de cong hai so nguyen */ int hamtinhtong ( int a , int b ) { printf ( "Gia tri cua a trong ham hamtinhtong() = %dn" , a ); printf ( "Gia tri cua b trong ham hamtinhtong() = %dn" , b ); return a + b ; } 

Run the above C program to compile and run to see the results:

Initialize global variables and local variables

When global variables and local variables are defined, it is not initialized by the program, it must initialize by yourself. Global variables usually initialize automatically by the program when you define them as follows:

Data type Default value 0 char '' float 0 double 0 pointer NULL

Often in practice programming you should initialize variable values ​​correctly, otherwise your program may cause unwanted results.

According to Tutorialspoint

Previous article: Function in programming C

Next lesson: Array in Language C

5 ★ | 1 Vote