Linux C Programming Tutorial Part 10 - Variable Scopes

If you are following our C programming tutorial series, you should be aware of the concept of variables. While we've discussed the basics of variables, there's another important aspect related to variables that we'll be discussing here: scope of variables

Let's start with the swapping values code that we used in one of our previous tutorials:

#include <stdio.h>

void swap (int val1, int val2)
{
int temp = 0;

temp = val1;
val1 = val2;
val2 = temp;

printf("\nSwapped values are: %d and %d", val1,val2);

}

int main()
{
int a=0, b=0;

printf("Enter two integer values\n");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(a,b);

return 0;
}

Here, in this piece of code, variables 'val1' and 'val2' have local scope, meaning they come to life when the function 'swap' gets called and they die as soon as the call to 'swap' is over. You just can't access 'val1' and 'val2' before or after the call to function 'swap'. Similarly, scope of variables 'a' and 'b' is also local - local to function 'main'.

Note that these local scope variables are also known as automatic variables.

Now, while variables with local scope are limited to the block in which they are declared, there is another type of variables whose scope is global. As the name suggests, global scope variables can be used across functions. For example, the variable 'var' is a global integer variable, and can be used in both 'main' and 'swap' functions.

#include <stdio.h>

int var;

void swap (int val1, int val2)
{
int temp = 0;

temp = val1;
val1 = val2;
val2 = temp;

printf("\nSwapped values are: %d and %d", val1,val2);

}

int main()
{
int a=0, b=0;

printf("Enter two integer values\n");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(a,b);

return 0;
}

By default, the value '0' is assigned to global variables. But that's not the case with local variables - you need to assign a value to them when they are defined, otherwise they hold a garbage value. For example, in the following program:

#include <stdio.h>

int var;

int main()
{
int a;

printf("Local variable 'a' currently holds: %d", a);

printf("\n Global variable var currently holds: %d", var);


return 0;
}

There are high chances that you'll get a non-zero value for 'a'. 'var' on the other hand, will always be zero in the beginning. 

Moving on, can there be global and local variables of same name? Well, the answer is yes. Ok, then what will the following piece of code produce in output:

#include <stdio.h>

int var = 5;

int main()
{
int var = 10;

printf("Local variable 'a' currently holds: %d", var);

printf("\n Global variable var currently holds: %d", var);


return 0;
}

Well, the output will be '10' in both cases. Reason being, the local variable 'var' will override the global 'var'. So now the next logical question is, how to access the global 'var' inside 'main' in this case? Sadly, the answer is NO. In fact, you should avoid getting into situations like these when working with C language.

Next comes the concept of 'extern' variables. In layman's term, by using the keyword 'extern' with any variable declaration, you're telling the compiler that this variable is already declared/defined somewhere else and we're just using it here. For example, in the following piece of code, the compiler doesn't know that 'var' exists when it tries to compile the printf statement inside the main function.

#include <stdio.h>

int main()
{

printf("\n Global variable var currently holds: %d", var);


return 0;
}

int var = 5;

That's why you get an error like the following during compilation:

main.c: In function ‘main’:
main.c:14:58: error: ‘var’ undeclared (first use in this function)
printf("\n Global variable var currently holds: %d", var);
^
main.c:14:58: note: each undeclared identifier is reported only once for each function it appears in

But if you declare 'var' as extern here, you'll see everything works fine. That's because the compiler gets to the original 'var' declaration eventually.

#include <stdio.h>

int main()
{
extern int var;

printf("\n Global variable var currently holds: %d", var);


return 0;
}

int var = 5;

And you get the correct output:

Global variable var currently holds: 5

So this is how extern works. Extern variables are often used when your program/project is split over multiple source code files and you want to, say, use in 'file1', a variable defined in 'file2'. 

And finally, as we are discussing variable scopes, it'd be better if we discuss 'static' variables here as well. Static variables are special in the sense they retain their value even after going out of scope. This means they are initialized only once, which is the first time.

static int counter 

Following is a piece of code that uses a static variable to count the number of times a function gets called.

#include <stdio.h>

int swap (int val1, int val2)
{
static int counter = 1;
int temp = 0;

temp = val1;
val1 = val2;
val2 = temp;

printf("\n Function called %d times\n", counter);
counter++;
}

int main()
{
int a=9, b=4;

swap(a,b);
swap(a,b);
swap(a,b);

return 0;
}

Here's the output:

 Function called 1 times 

Function called 2 times

Function called 3 times

So you can see, the variable 'counter' retained its value even after going out of scope. Just like global variables, static variables also have a default value of '0'. 

Conclusion

In this tutorial, we discussed several important concepts, all related to scope of variables in the C programming language. Don't forget to create your own programs to better understand the difference between extern, static, global, and local variables. And as always, in case of any doubt or query, drop in a comment below.

Share this page:

0 Comment(s)