Local,Static and Global variables in C

local static and global variables

In this article, I will explain the local static and global variables in C. Basically variable defines a location name where you can put value and you can use these value whenever required in the program.

You can also read below Articles,

1. Introduction of data types
2. Variable in C
3. Storage Class introduction
4. Linkage In C
5. Memory Layout of C program

Local Variable in C:

The local variable is a variable which is declared within a function, block (within curly braces) or function argument. Consider the below program,

In the above program, a, x and y are local variables. They only accessible within the test function when control comes out from the function then they will destroy automatically.

If you will create a variable within the function and create the same name variable within the block (inside curly braces) in the same function, then both variable would be different. As soon as the block ends (curly braces) the variable which declared inside the block is destroyed. See the below code,

local variable in c

 

Some important points related to the local variable

 

1. The stack contains non-static local variables from functions and related book-keeping data.

2. As soon as the block ends (curly braces) the variable which declared inside the block is destroyed and it will not visible to the outside of the block. See the below code, when you will compile the below code then you will get the error.

 

3. If you will not mention storage class with a local variable, then the auto storage class will attach with a local variable.

4. Never return the address of the local variable from the function. It will be the causes of the dangling pointer.

Let see the below programming example,

In the below code, Data (variable) is destroyed when control comes outside the function. So if you try to read the value of Data using the pointer after calling the Fun() you will get an undefined result.

So in the below code piData is a dangling pointer that is pointing a memory which is not available.

 

Note: None linkage is associated with the local variable.

 

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

Static Variable in C

A static variable preserves its previous value and it is initialized at compilation time when memory is allocated. If we do not initialize the static variable, then it’s a responsibility of compiler to initialize it with zero value.

Syntax: static Data_type Variable_name;

Let see the below program, in which I have created static variables.

 

Some important points related to the static variable

 

1. If we have used the static keyword with a variable or function, then only internal or none linkage is worked.

2. A static variable lives throughout the program but there is scope in the module in which it has been declared. See the below program in which I have created a local static variable data1. The life of data1 is throughout of the program but it will visible only test function.

 

3. A global variable with static keyword has internal linkage, so it only accesses within the translation unit (.c). It is not accessible by another translation unit. The static keyword protects your variable to access from another translation unit.

4. Static variables are initialized as 0 if not initialized explicitly.

5. Initialized static variable creates in DS and uninitialized static variable creates in BSS.

6. By default in C language, linkage of the function is external that it means it is accessible by the same or another translation unit. With the help of the static keyword, we can make the scope of the function local, it only accesses by the translation unit within it is declared.

7. A static variable only initializes once, so a variable declared static within the body of a function maintains its prior value between function invocations.

In the below program, you can see the value of data persist between the different function call.

static variable in c



Global Variable in C

Variables which declared outside the function are called global variables. A global variable is not limited to any function or file it can be accessed by any function or outside of the file. If you have not initialized the global variables, then it automatically initialized to 0 at the time of declaration.

See the below program, the data variable is a global variable.

 

 

Some important points related to the global variable

 

1. Unlike local variables, global variables are not destroyed as soon as the function ends.

2. Global variables are initialized as 0 if not initialized explicitly.

3. Initialized global variable creates in DS and uninitialized global variable creates in BSS.

4. By default, all global variable has external linkage.

Suppose in a program there are two files Driver.c and Calculation.c. Now the requirement is to share an identifier between these two files to get the result. I have written down a sample code to describe the importance of the global variable.

Driver.c 

Calculation.c

 

For More detail see this article, Importance of the linkage.

 

Some important program related to the variable which asks by your interviewer:

Q1: 

Answer ?



Q2:

Answer ?

Q3:

Answer ?

Q4:

Answer ?

Q5:

Answer ?



Q6:

Answer ?

Q7:

Answer ?

Q8:

Answer ?

Q9:

Answer ?

Q10:

Answer ?

Q11:

Answer ?



Q12:

Answer ?

Q13:

Answer ?

Q14:

Answer: ?

Don’t Forget to write your answer in commentBox.

 



Leave a Reply