C Language Fundamentals: Scope of Variables

        In this section we will learn about the scope of variables. In the previous lessons we already knew how to define variables, assign and evaluate them, but did not say anything about their scope. In fact, the variable we define will not always be valid, it will only be valid within its scope and invalid outside its scope.

1. Local variables

        We first define some variables inside the main function and use them, for example:

int main(int argc, char *argv[])
{
	int a;
	int b;
	int c;
	//do something
	return 0;
}

        Variables like this in a function (not only the main function, all functions are the same) are called local variables, and their scope is inside the current function. That is, these variables can only be used inside it, not in the function External use.

        The same structured statements if else, while, do while, for, switch, etc., variables can also be defined inside these structures, for example:

if (is_true)
{
	int a;
	int b;
	//do something
}
else
{
	int c;
	int d;
	//do something
}

while (is_true)
{
	int a;
	int b;
	//do something
}

for (int i = 0; i < 10; i++)
{
	int a;
	int b;
	//do something
}

switch (val)
{
	case 1:
	{
		int a;
		int b;
		//do something
		break;
	}
	case 1:
	{
		int a;
		int b;
		//do something
		break;
	}
	default:
	{
		int a;
		int b;
		//do something
		break;
	}
}

        These variables inside the structured statement are scoped inside the structured statement. Note that the scope of variables defined inside a case in switch is the current case. We can simply understand that a variable defined inside the curly braces has its scope within the current curly braces and is invalid outside the current curly braces. The same is true for both loop-nested and branched-nested programs. No further elaboration on this point.

        Note that when two variables of different scopes appear in a nested structure, the variable name can be the same, and its smaller scope prevails when used. E.g:

int main(int argc, char *argv[])
{
	int a;
	int b;
	//do something
	for (int i = 0; i < 10; i++)
	{
		int a;
		int b;
		//do something
		if (i == 4)
		{
			int a;
			int b;
			//do something
		}
	}
	//do something

	return 0;
}

        Such procedures are legal. But pay special attention to their scope when using variables with the same name in these different scopes. So we advise readers not to define variables with the same name when defining them.

 

2. Global variables

        The opposite of local variables is global variables. We call variables defined outside functions as global variables. The scope of these variables is the entire program, which means that all functions and structured statements can use them, such as

/* a.c */
int a;
int b;
	
int afunc(void)
{
    a = 2;
    b = a;
	//do somethine
	return 0;
}

        In addition, in the previous section, we have learned how to compile multiple source files together, so do global variables also take effect in other files? The answer is yes. When we define a global variable i in the ac file, and this variable cannot be used directly in another source file bc, we need to use the extern keyword to declare a file external variable outside the function

/* b.c */
extern int a;
extern int b;
	
int bfunc(void)
{
    a = 2;
    b = a;
	//do somethine
	return 0;
}

        In this way, the variables defined in ac can be used in bc. Note that extern int a; and extern int b; just tell the compiler that global variables outside the current file need to be used in the current file, rather than defining these new variables. Of course, these variables are also global in bc.


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691204&siteId=291194637