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 values 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 that is declared within a function, block (within curly braces), or function argument. Consider the below program,
void test(int x, int y) { int a; }
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 variables would be different. As soon as the block ends (curly braces) the variable which declared inside the block is destroyed. See the below code,
#include <stdio.h> int main(int argc, char *argv[]) { int a = 5; { int a = 10; printf("value of a = %d\n",a); } printf("value of a = %d\n",a); return 0; }
Output:
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.
#include <stdio.h> int main(int argc, char *argv[]) { { int a = 10; } printf("value of a = %d\n",a); return 0; }
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 cause 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 that is not available.
#include<stdio.h> int* Fun() { int Data = 5; //Local variable return &Data; //Address of local variable } int main() { //Returning address of the local variable int *piData = Fun(); printf("%d", *piData); return 0; }
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.
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 the responsibility of the 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.
#include <stdio.h> // Uninitialized global variable stored in BSS static int data1; //Initialized static variable stored in DS static int data2 = 0; int main(void) { // Uninitialized static variable stored in BSS static int data3; //Initialized static variable stored in DS static int data4 = 0; //Printing the value printf("data1 = %d\n",data1); printf("data2 = %d\n",data2); printf("data3 = %d\n",data3); printf("data4 = %d\n",data4); return 0; }
Output:
data1 = 0
data2 = 0
data3 = 0
data4 = 0
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 the program but it will visible only test function.
#include <stdio.h> void test() { //Initialized static variable stored in DS static int data1 = 0; printf("%d\n",data1); } int main() { test(); return 0; }
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, the 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.
#include<stdio.h> int test() { static int data = 0; data++; return data; } int main() { printf("data = %d\n", test()); printf("data = %d\n", test()); printf("data = %d\n", test()); return 0; }
Output:
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.
#include<stdio.h> int data; // global variable int main() { printf("data = %d\n", data); return 0; }
Output:
data = 0
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
#include <stdio.h> int Amount = 0; //External Linkage int main() { Addition(); //function define in other file. printf("%d\n", Amount); //Display amount return 0; }
Calculation.c
extern int Amount; void Addition() { int a = 0, b = 0; printf("Enter the value\n"); scanf("%d%d",&a,&b); Amount = a + b; }
For more detail see this article, Importance of the linkage.
Some important program related to the variable which asks by your interviewer:
Q1:
#include <stdio.h> int main() { printf(" Data = %d \n", data); return 0; }
Answer:?
Q2:
#include <stdio.h> int main() { int data = 6; int data = 27; printf("Data %d\n", data); return 0; }
Answer:?
Q3:
#include <stdio.h> int main() { int main = 6; printf("%d", main); return 0; }
Answer:?
Q4:
#include<stdio.h> int main() { extern int data; data = 1993; printf("%d",data); return 0; }
Answer:?
Q5:
#include <stdio.h> int main() { int data = 30; { int data = 10; printf("%d",data); } return 0; }
Answer:?
Q6:
#include<stdio.h> int main() { { int a = 10; } printf("value of a = %d\n",a); return 0; }
Answer:?
Q7:
#include<stdio.h> int main(void) { extern int var; var = 10; printf("%d",var); return 0; } int var = 0;
Answer:?
Q8:
#include<stdio.h> int main(void) { extern int var; printf("%d",var); int var = 0; return 0; }
Answer:?
Q9:
#include<stdio.h> int init() { return 10; } int main() { static int data = init(); printf(" value of data = %d", data); return 0; }
Answer:?
Q10:
#include<stdio.h> int main() { static int data; printf("%d \n", data); return 0; }
Answer:?
Q11:
#include<stdio.h> int test() { int data = 0; data++; return data; } int main() { printf("data = %d\n", test()); printf("data = %d\n", test()); return 0; }
Answer:?
Q12:
#include<stdio.h> int test() { static int data = 0; data++; return data; } int main() { printf("data = %d\n", test()); printf("data = %d\n", test()); return 0; }
Answer:?
Q13:
#include <stdio.h> int main(void) { int * piData; { //block int Data = 27; piData = &Data; } printf("piData = %d\n", *piData); return 0; }
Answer: ?
Q14:
#include<stdio.h> int *Fun() { int Data = 5; //Local variable return &Data; //Address of local variable } int main() { //Returning address of the local variable int *piData = Fun(); printf("%d", *piData); return 0; }
Answer: ?
Q15:
Can we access a global variable if there is a local variable with the same name in C?
Answer: ?
Q16:
Can we Redeclare the global variable twice in C?
Answer: ?
Q17:
Can Global Variables be dangerous and we should avoid it?
Answer: ?
Q18:
How linkers resolve global symbols define at multiple places in C application?
Answer: ?
Q19:
What is the difference between static variables and register variables in C?
Answer: ?
Q20:
What are the default values of static variables in C?
Answer: ?
Q21:
How are variables scoped in C – Static or Dynamic?
Answer: ?
Q22:
What is the difference between the internal static variable and the external static variable in C?
Answer: ?
Q23:
Can we use extern and static keywords together?
Answer: ?
Don’t forget to write your answer in the comment box.
Recommended Articles for you:
- Introduction of internal, external and none linkage in C
- Memory Layout of C program
- Use of typedef in C
- C macro, you should know
- typedef vs #define in c, you should know
- Use of enum in C programming
- The character set of C Language
- Format specifiers in C
- Data types in C
- Variable in C
- Storage class in C
- Const Qualifier in C Language
- The understanding of volatile qualifier in C
- Use of volatile and const keyword together