Variables in C Language
In simple terms a variable is a storage area which has some memory allocated to it. Each variable has a specific type, which specifies size and layout of variable's memory. The name of a variable can be composed of upper and lowercase letters, digits, and the underscore character.
Let's see the syntax to declare a variable in the C language:
Syntax of Variable Declaration:
data_type variable_name;
or
data_type variable_name1, variable_name2, variable_name3;
Examples :
int a, digit=5;
char letter='A';
float money, area;
double x;
/* initialization of variable*/
a = 10;
x = 26.5;
Scope of Variables in C Language
When we declare a variable in a program, it can be accessed based on their scope. The scope of a variable decides in which portion of a program, the variable can be accessed. The scope of a variable defines the visibility of variable in the program and it depends on the position where variable is declared. There are five types of variables in C:
1. Local Variable - A local variable needs to be declare and use inside the function or program block. It must be declared and initialized at the start of the block before use.
#include<stdio.h> void main(){ void add(); int a = 10, b = 20; printf("A = %d, B = %d", a, b); add(); } void add() { int sum ; sum = a + b ; printf("\nResult = %d", sum) ; } Output: A = 10, B = 20 Result = 0In the above example, the variables 'a' and 'b' are declared inside the function main(). So, they can be used only inside main() function and not in add() function.
2. Global Variable - Global variables are defined on top of the program, generally outside of a function and they can be accessed inside any of the functions defined for the entire program after its declaration.
#include<stdio.h> int a, b; /* global variable declaration */ void main(){ void add(); a = 10; b = 20; printf("A = %d, B = %d", a, b); add(); } void add() { int sum ; sum = a + b ; printf("\nResult = %d", sum) ; } Output: A = 10, B = 20 Result = 30
3. Static Variable - A variable is declared with the static keyword that retains its value between multiple function calls is known as static variable.
#include<stdio.h> void main(){ void increment(); int a = 10, b = 20; //local variable increment(); increment(); increment(); printf("A = %d, B = %d", a, b); } void increment() { int a=10; //local variable static int b=20; //static variable a=a+10; b=b+10; printf("A = %d, B = %d", a, b); } Output: A = 20, B = 30 A = 20, B = 40 A = 20, B = 50 A = 10, B = 20If we call increment() function many times, the local variable will print the same value for each function call, e.g, A = 20,20,20 and so on. But the static variable will print the incremented value in each function call, e.g. B = 30,40,50 and so on. In main function printf() will print value of local variables, e.g. A = 10, B = 20.
4. Automatic Variable - In C language, all variables that are declared inside the program block or functions, are automatic variables by default. The scope of automatic variables are same as local variables. Automatic variable can be explicitly declared using auto keyword.
#include<stdio.h> void main() { int a = 10; //local variable similar as automatic auto int b = 20; //automatic variable printf("A = %d, B = %d", a, b); } Output: A = 10, B = 20
5. External Variable - External variables are also known as global variables. These variables are defined outside the function using extern keyword and can be shared between multiple C files. headerfile.h
extern int count; void printHello();sourcefile.c
int count = 0; void countFunction() { count++; // do some other coding here }main.c
#include <stdio.h> #include "headerfile.h" #include "sourcefile.c" void main() { int i; printf("\"countFunction\" are called %d times.\n", count); for(i = 0; i < 5; i++) { countFunction(); printf("\"countFunction\" are called %d times.\n", count); } } Output: countFunction are called 0 times. countFunction are called 1 times. countFunction are called 2 times. countFunction are called 3 times. countFunction are called 4 times. countFunction are called 5 times.