A variable represents a memory location that stores the data. For example: an int variable num has a value 10 (int num = 10), here the variable name is "num"
that represents the location in the memory where this value 10
is stored. As the name suggests, the value of a variable can be changed any number of times.
Syntax – Declaring a variable
data_type variable name;
For example:
//a variable num of int type
int num;
//two variable ch1 and ch2 of char type
char ch1, ch2;
//three variable x, y and z of float type where y has been
// initialized with a value and other variables x & z are
// un-initialized.
Float x, y=10.5, z;
Example: Variables in C
#include <stdio.h>
int main()
{
int num1 = 20, num2 = 50;
char ch = 'A';
float x = 10.5, y = 13.5;
printf("Variable 'ch' value: %c\n", ch);
printf("Variable num1 and num2 values: %d\t%d\n", num1, num2);
printf("Variable x and y values: %f\t%f\n", x, y);
return 0;
}
Output:
Rules for defining a variable
1. A variable name can has alphabets, digits and underscore.
2. Variable name cannot start with the digit, it can start with alphabets and underscore only. For example, _num
and num_1
are valid variable names, however 9num
& 99_num
are not valid variable names.
3. Variable name cannot contain whitespaces. For example: num 1
, num 2
, my var
are not valid names as they contain whitespaces.
4. A variable name cannot be a keyword or reserved words. For example, “if”, “else”, “int” are invalid names as these are the keywords.
Types of variable in C
1. Local variable
A variable that is declared inside a function or a block is called a local variable as the scope of this variable is limited. A variable declared inside a function can only be accessed inside that function, its scope is limited to that function and cannot be accessed outside that function.
Similarly a variable declared inside a block cannot be accessed outside that block.
Note: Local variable must be initialized before it can be used.
Local Variable Example:
#include <stdio.h>
void myfunction() {
// This is a local variable
int num = 99;
printf("Local variable num value: %d", num);
}
int main()
{
myfunction();
}
Output:
Local variable num value: 99
2. Global variable
A variable is called global variable, if it can be accessed by all the functions and blocks of the program. The scope of the global variable is not limited to the particular function or block, its scope is limited to the program. Which means a global variable can be accessed inside a program but cannot be accessed outside the program.
Note: It is declared at the start of the program
Global Variable Example:
#include <stdio.h>
//This is a global variable
int num = 99;
void myfunction()
{
printf("%d\n" , num);
}
void myfunction2()
{
printf("%d\n" , num);
}
int main() {
myfunction();
myfunction2();
return 0;
}
Output:
99 99
3. Static variable
A static variable is declared with the static keyword. It retains its value during multiple function calls. Static variables are initialized only once. The compiler retains their values till the end of the program. Static variables can be declared inside or outside a function. The default value of static variables is zero.
Static Variable Example:
In the following example, we have a local variable and a static variable declared inside a function. In the main() method, we are calling the function repeatedly, since there is a print statement in the function, the value of num and num2 gets printed on each function call. As you can see in the output that the local variable value is same for each function call, while static variable retained its value from previous function call.
#include <stdio.h>
void myfunction(){
//This is a local variable
int num = 20;
//This is a static variable
static int num2 = 20;
num = num + 100;
num2 = num2 + 100;
printf("\n Value of num: %d, value of num2: %d",num,num2);
}
int main() {
//Calling myfunction() repeatedly to print num and num2
//values for each repeating function calls.
myfunction();
myfunction();
myfunction();
myfunction();
return 0;
}
Output:
4. Automatic variable
Automatic variables are similar to the local variables, their scope is limited to the particular function or block. The local variables inside function or block are automatic variables by default. We can explicitly mark a variable automatic by using auto keyword.
Automatic Variable Example:
void function()
{
//This is also an automatic variable by default
int num = 100;
//This is how you can explicitly declare variable as automatic
auto int num2 = 150;
}
5. External variable
External variables have wider scope than global variable. The scope of a global variable is limited to the particular c file, which means it cannot be accessed by another c file. However external variables can be accessed by other C files, these variables are shared between multiple c files. We can declare an external variable using extern keyword.
External Variable Example:
This is how you can declare a variable as external, using extern keyword. Since the scope of extern is greater than the scope of global variable, the extern variable can be accessed by all functions and blocks of the same C file, just like global variables. Unlike global variable, external variables can be accessed outside the C file. An extern variable declared in a C file can be accessed by other C file.
extern int num = 99;