Variables in C

Variables in C


What is Variable ?

Variable is a data name which is used to store some data value or symbolic names for storing program computations and results. Variable is a way to represent memory location to be easily identified. 

In this tutorial you will learn :


Rules for naming Variables :

1. First character must be started with a letter or an underline not with a digit. The remaining are of an alphabet, any numeric digit or the underscore character.
2. Variable names must be unique.
3. Blankspaces and commas are not allowed within a Variable.
4. Keywords can't be used as Variable.
5. Variable type can be int, char, float, etc.
6. Variables are case sensitive. A variable name can consist of 52 alphabetic characters (both upper and lower case), the underscore character ( _ ) and 10 digits (i.e., 0-9).

Declaration of Variable :

Variables must be declared before they are used in the program. During the declaration, memory space is not allocated for the variable. It only happens during the variable definition.

Reason behind variable declaration :
1. To inform the compiler about the variable's name.
2. To specify the datatype of the variable.

Syntax :
datatype variableName;

Example :
int anik,she;
char rias, mizuhara;

//Here, int and char are datatypes. 
//Anik, she, rias, mizuhara are variable's name.

Variable Initialization :

Variable initialization is nothing but assigning a value to the variable.

Syntax :
datatype variableName = value;

Example :
int deb = 91;
float arijit = 211.169;
char sovan = 'K';

//Here, int, float and char are datatypes. 
//91, 211.169, K are value assigned to the variables.

Types of Variables :

C programming language consists of 5 types of variables. They are :
1. Local Variable
2. Global Variable
3. Static Variable.
4. Automatic Variable.
5. External Variable.

1. Local Variable :

A variable which is declared inside the function is known as Local Variable. These variables are declared within the function and cannot be accessed outside the function. Local variables must have to be initialized before it's use.

Example :
#include<stdio.h>
void eruditors();
int main()
{
   int aqua = 56, kazuma = 117; // aqua and kazuma are local variables of main function. These variables are not visible to eruditors funtion.
   // accessing lucy and natsu in this function will throw 'lucy' and 'natsu' are undeclared.

   printf("\nvalues of aqua = %d and kazuma = %d", aqua, kazuma);
   eruditors();
}
 
void eruditors()
{
   int lucy = 47, natsu = 91; // lucy and natsu are local variables of test function.
   // accessing aqua and kazuma in this function will throw 'aqua' and 'kazuma' are undeclared.     
        
   printf("\nvalues of lucy = %d and natsu = %d", lucy, natsu);
}

Output :
values of aqua = 56 and kazuma = 117                                 
values of lucy = 47 and natsu = 91

Keypoints of Local Variable :

~ By default, local variables are uninitialized and it contains garbage value.
~  It is visible only inside their function, and only function's inside statements can access that local variable.
~ It is declared when control enters a function and gets destroyed, when control exits from function.

2. Global Variable :

A variable which is declared outside the function is known as Local Variable. These variables are defined outside the main function and so it can be accessed from anywhere in the function. Local variables must have to be declared before its' use.

Example :
#include<stdio.h>
void eruditors();
int eren = 117, mikasa = 56;
int levi = 211, annie = 101;

//eren, mikasa, levi, annie all are global variables.

int main()
{
   printf("All the variables are accessable from main function");
   printf("\nvalues of eren = %d\nmikasa = %d\nlevi = %d\nannie = %d", eren, mikasa, levi, annie);
   eruditors();
}
 
void eruditors()
{
   printf("\nAll the variables are accessable from eruditors function");
   printf(" \nvalues of eren = %d\nmikasa = %d\nlevi = %d\nannie = %d", eren, mikasa, levi, annie);
}

Output :
All the variables are accessible from main function                  
values of eren = 117                                                 
mikasa = 56                                                          
levi = 211                                                           
annie = 101                                                          
All the variables are accessible from eruditors function             
values of eren = 117                                                 
mikasa = 56                                                          
levi = 211                                                           
annie = 101

Keypoints of Global Variable :

~ Global variables are declared outside of any function.
~ Unlike local variable, global variables retain their values between function calls and throughout the program execution.
~ Global variables can be used by any piece of code as it is visible to every function.

3. Static Variable :

A variable which is declared with the static keyword is known as Static Variable.  

Syntax :
static datatype variableName = value;

Example :

#include<stdio.h>

// Function declaration
void eruditors();

int main()
{
    eruditors();
    eruditors();
    eruditors();
    eruditors();
    eruditors();
    eruditors();
    return 0;
}

// Function definition 
void eruditors()
{
    int var1 = 56;
    static int var2 = 56;

    printf("Local var1 = %d, Static var2 = %d\n", var1, var2);

    var1++; // Increment local variable 
    var2++; // Increment static variable
}

Output :


Keypoints of Static Variable :

~ This variables are known to retain the value even after they exit the scope.
~ If this function is called multiple times, the Local Variable will print the same value for each function call. But the Static Variable will print the incremented value. Follow the above example.
~ A static variable is initialized with a default value which is either 0 or NULL.

4. Automatic Variable :

A variable that is declared inside a block is called Automatic Variable. The automatic variable can be declared in any user define function in the starting of the block.

Syntax :
auto datatype variableName;

Example :
#include<stdio.h>
int main()
{
	int anik=56; //local variable (also automatic)
	auto int deb = 47; //automatic variable.
	printf("value of anik is : %d\nand value of deb is : %d",anik,deb);
	return 0;
}

Output :
value of anik is : 56                                                
and value of deb is : 47

Keypoints of Automatic Variable :

~ Automatic variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block.
~ These variables have local scope to that block. That means these can be accessed in which variable declared.
~ We can declare automatic variables without using 'auto' keywords.

5. External Variable :

External variables are defined outside the function and are used to share a variable in multiple C source files. These variables are also known as Global Variables and are available globally throughout the function execution.

Syntax :
extern datatype variableName;	//variable declaration.
extern datatype functionName();	//function declaration.

Example :

#include<stdio.h>
extern int she = 35;
int he = 98;
int main() 
{
   extern int he;
   printf("The value of extern variables 'she' and 'he' are : %d,%d\n",she,he);
   she = 211;
   printf("The value of modified extern variable 'she' is : %d\n",she);
   return 0;
}

Output :
The value of extern variables 'she' and 'he' are : 35,98             
The value of modified extern variable 'she' is : 211