In c language, variables have global variables and local variables , which are similar to many high-level languages such as C#,java. However, the local variables in the C#,java are not allowed to be the same as global variable names in the scope of global variables, and the C language is allowed to do so. This is highly discouraged, but if C is allowed to do so, analyze the value of the variable whose variable name is actually called when the variable name is called at different locations.
The code is as follows:
#include <stdio.h>//Global Variables//scope: Start from definition to end of file//default initial value is 0int c = 1;intTest () {int c = 2; //this position is int c = 1; int c = 2; all works .//The last C = 2 of the scope is used;//2 printf ("%d\n" , c);}//When there are more than one variable with the same name when calling a variable//The variable that uses the scope closest to itself//133423intMain () {//this position only you int c = 1;//1 printf ("%d\n", c); //Local Variables//Scope : From definition start to function end//no default initial value int c = 3; //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3 printf ("%d\n" , c); { //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3 printf ("%d\n", c); //Local Variables//scope: Start from definition to end of code block//no default initial value int c = 4; //this position int c = 1; int c = 3;int c = 4; all works .//The last C = 4 of the scope is used;//4 printf ("%d\n" , c); } //view inside the test () function//2test (); //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3 printf ("%d\n", c); return 0;}
Results:
Summary: When more than one variable with the same name is in effect when calling a variable, use the scope closest to the variable
Use of c global variables and local variables (when variable names are the same)