The scope of a variable defines the region in the code where that variable can be referenced or used. It includes the current state of the variables along with their values. The scope of a variable depends on where it is declared in the program. Once declared, the variable can be accessed and manipulated as needed. Understanding how a variable's value is accessed and used within its scope is crucial for determining the program's behavior.
When the program exits the scope where a variable is defined, the memory allocated to that variable is typically released, rendering the variable inaccessible. This efficient management of resources prevents accidental access or modification of the variable’s data outside its intended scope.
In this section, you'll learn how local variables work in C. You'll first code a couple of examples, and then you'll generalize the scoping principle.
Example 1:
#include <stdio.h>
int main()
{
int my_num = 7;
{
//add 10 my_num
my_num = my_num +10;
//or my_num +=10 - more succinctly
printf("my_num is %d",my_num);
}
return 0;
}
Let's understand what the above program does.
In C, you delimit a block of code by {} . The opening and closing curly braces indicate the beginning and the end of a block, respectively.
The main() function has an integer variable my_num that's initialized to 7 in the outer block. There's an inner block that tries to add 10 to the variable my_num. Now, compile and run the above program. Here's the output:
//Output
my_num is 17
You can see the following:
Example 2:
#include <stdio.h>
int main()
{
int my_num = 7;
{
int new_num = 10;
}
printf("new_num is %d",new_num); //this is line 9
return 0;
If you try compiling the above code, you'll notice that it doesn't compile successfully. And you'll get the following error message:
This is because the variable new_num is declared in the inner block and its scope is limited to the inner block. In other words, it is local to the inner block and cannot be accessed from the outer block.
Based on the above observations, let's write down the following generic principle for local scoping of variables:
{/*OUTER BLOCK*/
{
//contents of the outer block just before the start of this block
//CAN be accessed here
/*INNER BLOCK*/
}//contents of the inner block are NOT accessible here
}
In the previous example, you learned how variables inside the nested inner block cannot be accessed from outside the block.
In this section, you'll understand the local scope of variables declared in different blocks.
#include <stdio.h>
int main()
{
int my_num = 7;
printf("%d",my_num);
my_func();
return 0;
}
void my_func()
{
printf("%d",my_num);
}
In the above example,
Now compile and run the above program. You'll get the following error message:
If you notice, on line 13, the function my_func() tried accessing the my_num variable that was declared and initialized inside the main() function.
We can represent this notion of local scope generically as follows:
{
/*BLOCK 1*/
// contents of BLOCK 2 cannot be accessed here
}
{
/*BLOCK 2*/
// contents of BLOCK 1 cannot be accessed here
}
So far, you've learned about local scope of C variables. In this section, you'll learn how you can declare global variables in C.
Let's start with an example.
#include <stdio.h>
int my_num = 7;
int main()
{
printf("my_num can be accessed from main() and its value is %d\n",my_num);
//call my_func
my_func();
return 0;
}
void my_func()
{
printf("my_num can be accessed from my_func() as well and its value is %d\n",my_num);
}
In the above example,
This program compiles without any error, and the output is shown below:
In this example, there are two functions – the main() and my_func().
However, the variable my_num is not local to any function in the program. Such a variable that is not local to any function is said to have global scope and is called a global variable.
This principle of global scope of variables can be summarized as shown below:
//all global variables are declared here
function1()
{
// all global variables can be accessed inside function1
}function2()
{
// all global variables can be accessed inside function2
}
119 docs|30 tests
|
1. What is the full form of GATE exam? |
2. How many papers are there in the GATE exam? |
3. Can candidates from non-engineering backgrounds appear for the GATE exam? |
4. What is the validity period of a GATE score? |
5. How many times can a candidate attempt the GATE exam? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|