That’s how the variables can be used across the files, the scope 2 !
Welcome to Newbie Programmer Series. In a previous post (click here), we have discussed how can we use variables inside and outside of a function. Then in the last post (click here), we have discussed how the C language itself works. We have discussed the process of compilation, linking etc and a little about something called header files. But we got doubt at the end that how it is possible to use variables across files and using many “.c” files for a single program. So, This post that I am discussing now is the result of both. We will discuss how to use same variable across many “.c” files. That is, Scope of the Variables across files.
So if you are new to this series, please go to the Index (click here) and read out all the previous parts so that you can easily understand what I am writing about !
This time, lets do practically, theory later… its boring.
So I assume that you are using codeblocks as I have instructed to setup in a previous post of mine : Setting up C of the Computer (click here). We will first learn how to create a project then will do the coding stuffs.
As we are using multiple files for a single program, it’s better to work on it as a project. This will make it easy to handle many files. So follow the below steps to make a project in Codeblocks :
Congo ! you have successfully created a new Project, named testprj.
We have just setup an “empty” project. Now its time to add some files.
So until now, we have created a project named “testprj” and then added two files namely “test.c” and “fun.c”.
Above the area where we write code, you can see the name of both of the files. You can click their names to switch between them.
Now let us add some code for these files.
For the file “test.c”, write this :
//test.c
#include <stdio.h>
main()
{
int num;
printf("%d", num*num);
}
This code is supposed to print square of num on the screen. You run it and you will get some stupid number. I got 4. That is because I did not assigned any value for num. So it takes up any value, square it and print. So either we can assign something like num = 5, or let’s do something different.
Lets assign the value of num in a different file, in “fun.c”
//fun.c
int num = 5;
So we are using this num variable in test.c but assigned the value in “fun.c” file.
But still, on running it, we are getting the same, some stupid answer, what we should be getting is 25 !
The problem is every time we run, each time, main function creates a new “num” variable and use it. And so do not check in any other files.
So somehow, we have to tell our program : “hey don’t create num variable again, please search, it’s already assigned in some other file”
To do this, use the thing called extern
extern means the same as I want to told ( in the above quoted bold sentence ).
Usage is simple :
extern (whatever to search)
So To say : “hey don’t create num variable again, please search, it’s already assigned in some other file”
We will write :
extern int num; So in the test.c file, in the place of “int num;”, write “extern int num;”. So that it can pickup the value from “fun.c” file.
Now run it, And the output is 25 !
How it works is like whenever computer finds “extern”, it will search other files for the value and will take that !
So we have discussed three cases. First we simply use the test.c file and got something stupid. Then we declared num variable in fun.c but still got something stupid. Finally we used “extern” and thus it searched for num and got its value from fun.c and used it test.c. The thing is that in the C language, once the variable space is created, same thing is not created again and again so we can use it !
So we are successful in using variables across files ! We are using the num variable across two files. We can do this with many files. Just remember, use extern to search in different files, and please work on many files as a project to keep things simple.
This was just a basic introduction. We will do applications later when we will complete enough part of this stuff. I think that’s enough for today. We are gonna discuss more interesting stuffs in the next part. So stay connected.
Previous Chapter: « How the C language itself works ?
© Shubham Ramdeo, 2015-2025.
All Rights Reserved.