C Programming – Understanding the scope of a variable

When we define a variable in C, have you ever noticed the variable has a scope? Every variable you define in C, has a scope defined; you are not allowed to access the variable beyond it’s scope. That means, the variable will be visible only within the defined scope. Through this article we are going to discuss the scope of a variable.

Variables in Local scope or Block scope

When you define a variable in a block (group of statements enclosed in curly braces {}), the scope of the variable is within the block only. You are not allowed to use the variable outside the block. For example;

	{
		int value = 10;
		printf("%d\n", value);
	}
	printf("%d\n", value); // error C2065: 'value': undeclared identifier

Similarly, if you define a variable in for loop header, that variable can be visible only within the for loop; not outside of it. Here is an example; This is the common error, most of us do;

	for (int i = 0; i < 5; i++)
		printf("%d", i);
	printf("%d", i); // This throws an Error. The variable 'i' is defined in for loop only.

Variables in Function scope

The variable you defined within a function can be accessible only within the function. The function variables are not allowed to access outside the function.

void Display()
{
	int value = 10;
	printf("%d\n", value);
}
printf("%d\n", value); // This throws an Error.

Variables in Global scope

The variables defined in global scope are accessible from anywhere in the program. You have to be more cautious when you define the variables in global scope. When you are using multiple modules in your program, the global variables defined in the modules will be available in your program and these may cause to throw duplicate symbol errors when there is a name conflict between the global variables defined in your program with the global variables defined in the modules.

When you need to use the global variables defined in the module, from your program; you need to declare them with extern keyword. That indicates to the compiler that, the variable is defined in other module. Otherwise, you will get below Errors;

multiple definition of `<<variable name>>'

Or, Microsoft C/C++ compiler will throw the errors like below;

error LNK2005: _<<variable name>> already defined in <<module name>>
fatal error LNK1169: one or more multiply defined symbols found

Scope precedence

Local scope has the precedence than other scopes. When your program has multiple variables defined with same name in different scopes; the variables in the particular scope has the precedence. For example, if you define a variable in Global scope, and the same variable in function scope; within the function, the variable defined in the function scope has the precedence the Global variable.

Working Example – demonstrate the scope of a variable

Let’s put all together what we learned so far;

  • For this we need 2 files. One is module file where we define a global variable in a module and
  • Another one is our main program file.

module.c file

This file has only one statement, which is define the global variable;

// module.c
int value = -50;

We need to generate a object file, as there is no main method defined in this file;

Linux C compilerMicrosoft C/C++ compiler
$ cc -c module.cC:\> cl /c module.c

After compilation, you will see module.o created from Linux C compiler. And module.obj file generates when you compile the program using Microsoft C/C++ compiler.

Now we need to create our main program file;

scope.c file

The code of the file, looks like below;

Let’s compile our program. Remember that, we should link the module.obj / module.o object file to our main program.

Linux C compilerMicrosoft C/C++ compiler
$ cc module.o scope.cC:\> cl scope.c /link module.obj

Upon successful compilation, the main program generates an executable file (a.out Or scope.exe). By running the executable file, you will see below output;

Local variable: 10
Function variable: 20
Module variable: -50

// Malin

C Programming – Understanding the scope of a variable

Leave a Reply

Your email address will not be published.

Scroll to top