A variable name is a name given to a memory location, which is used to store the data. In simple, variable is a named representation of a memory location.

It’s a value, that can be changed and reused any number of times during the execution of the program.

How to Define C Variables

A variable definition (defining a variable) in the code, is to create memory for that variable. Variable definition in the program tells the compiler that, how much memory should create for that variable based on data type associated with it.

While defining a variable, we need to keep remember the following points.

  • A variable name can have alphabets (upper case or lower case), digits or underscore.  It can also be a combination of alphabets, digits or underscore.
  • The first character in the variable name should be an alphabet or underscore. It cannot start with a digit.
  • A variable name is always case sensitive.
  • A variable name cannot be a keyword or a reserved word (keywords and reserved words are already having a definite meaning and purpose in C language).
  • No commas or blanks are allowed within a variable name.
  • No special symbol other than an underscore can be used in a variable name.
  • Maximum allowable length of the variable name is 31 letters (though some compilers allows 247 characters). It is always advisable to use simple and meaningful variable names.

    

Examples of Valid Variable Names :

int a;        // a is the name of the variable that stores integer value
char var;       //var is the name of the variable that stores character value
float _a30var;     //_a30var is the name of the variable that stores floating value
int a,b;   // a and b are the names of the variables of integer type 

 

Examples of Invalid Variable Names :

int a b; // variable names should be separated by comma
int 4    //variable definition should end with semi colon 
char !var;  //variable name should not start with any special symbol other than underscore
int float;  //float is a keyword and cannot be used as a variable name

 Let us get some more clarity on this,

int a; is a definition
int a = 4; is a declaration

Declaration and Initialization of C Variables

Declaration of variable is nothing but the description of the variable i.e. name of the variable, type of the variable etc.

The type of the variable must be declared at the beginning of the program using C Datatypes.

Syntax : data_type variable_name;

Examples :

           int a,b;   
           char variable;
           float _var_name;

 

Initialization of variable is nothing but assigning some value to that variable.

Syntax : data_type variable_name=value;

Examples :

             int a=4;    
             int b=10, c=15;
             char var_name=’c’;

Initialization of C Variables can also be done after their declaration.


Example program : 

#include<stdio.h>
int main()
 {
   int a;
   a=10; 
   int b=25;
   printf("Sum of a+b = %d", a+b);
   return 0;   
 }

 

The output of the above  program is :

Sum of a+b = 35

Initialization of C variables can also be done through scanf();

Example program :

#include <stdio.h>
int main()
{
    int a; 
    printf("Enter the value of a: ");
    scanf("%d",&a);  
    printf("The value of a is = %d",a);
    return 0;
}

 

The output of the above program is :

Enter the value of a : 2

The value of a is = 2

When we have declared a variable, we have meant to tell the compiler about the variable i.e. its type and its name, and to allocate a memory cell for the variable. In short, declaration is simply to describe information about the variable.

 Most of the time, declarations are definitions. However, this may not always be the case. For example, consider a function. The declaration of prototype statement for a function tells the compiler about the function - its name, return type, and number and type of its parameters. A similar statement, the function header, followed by the body of the function, defines the function giving the details of the steps to perform the function operation.

Similarly, for external variables, these two operations may occur independently. Declaring a variable with extern keyword specifies that the variable is defined elsewhere, i.e. memory for this variable is already allocated in another file.   Thus no new memory is allocated. To declare an external variable, the keyword extern is used.

Example program for declaration, definition and initialization of C Variables :

#include <stdio.h>
extern int a, b;      //Variable declaration – declaring a and b variables of integer type with extern keyword
int main () 
{
   int a, b;               // Variable definition – defining a and b variables 
   int c;     
   a = 10;               //Variable initialization - initializing value 10 to variable a
   b = 20;
   c = a + b;
   printf("value of c : %d \n", c)
   return 0;  
}

 The output for the above program is :

value of c : 30

Types of Variables in C

C Variables in general can be classified in to three types.

  1. Local variables

  2. Global variables

  3. External variables

  1. Local Variables : Local variable is nothing but the variable which is confined to that specific block or function in which it is defined. It is always defined at the beginning of the block or function.
    #include<stdio.h>
    int main()
    {
     int a = 2;
     int b = 3;
     printf("%d\n",a+b);
    }
    

    Output for the above program : 5

  2. Global variables: Global variable is a variable that can be used by all the functions in the program. Global variable is always defined outside the functions

    #include<stdio.h>
                       //Global variables
    	int a;	
    	int b;
    	int add()
    	{
    		return a+b;
    	}
    
    	int main()
    	{
    		int c; // Local variable
    		a = 2;
    		b = 3;
    		c = add();
    		printf("%d\n",c);
    		return 0;		
    	}
    
    

    Output for the above program :5

  3. External variables: External variables are the variables that are available for all the C programs. We can access the external variables without any declaration and initialization. To declare an external variable we need to use extern keyword.

    Syntax : extern datatype variable_name;

    Example Syntax:

    extern int a=5;      // a is an external variable declared global in File1.h)  
    #include "File1.h"  
    #include <stdio.h>  
    void function1()
    {  
       printf("External variable is: %d", a);  
    }  
    

    Output for the above program :5