fuddlepixelFuddlepixel coz tech is fun();

Scope of the Variables Across Files

Let us discuss the scope of the variables, about how the variables can be used across the files in C language, the program as a project !

By Shubham Ramdeo
28 October 2015 · 5 mins read
# c  newbie 

Scope of the Variables Across Files

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.

Making a Project in CodeBlocks

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 :

  1. Run Codeblocks.
  2. Go to File > New > Project… A box will appear.
  3. Select Empty Project, double click on it or click on “Go” button.
  4. Another box will appear, click on next.
  5. Enter a name to our project in the Project Title field, say testprj.
  6. Then write a folder to create our project, say in “D:".
  7. In the last text field, I can see Resulting filename : “D:\testprj\testprj.cbp”. Now click next.
  8. Just check for GNU compiler or something, should be there. Click on Finish

Congo ! you have successfully created a new Project, named testprj.

We have just setup an “empty” project. Now its time to add some files.

  1. As you know how to make a simple empty file, so make it, let’s make test.c. Go to File > New > Empty File. A box will appear immediately asking you “Do you want to add this new file to the active project ?” Click yes.
  2. Now another box will appear to save it, save that file as “test.c”.
  3. A box will appear for Multiple Selection, Click on Select All, Both release and debug named option will get checked. Then click on OK. 
  4. Now make another file just following the above steps, but this time, name it as “fun.

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.

Variables Across 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.

How extern comes into play !

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.


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top