C Programming Variables

A variable in C is the name given to a memory location, where a program can store data. To uniquely identify a memory location, each variable must have a unique identifier. A program can store and manipulate the value stored using variable's identifier. Variables must be defined before their use in a program.

Rules for Writing Variable Name in C
  • A variable's name can be composed of alphabets, digits, and underscore only.
  • The first character of a variable name must be either an alphabet or underscore.
  • Variable name is case sensitive. Home and home is recognised as two separate identifiers.
  • Any special characters other than alphabets, digits, and underscore (such as :, . ,blank space, / are not allowed).

Declaration and Initialization of Variable in C

A variable can be of any data type like char, int, double etc. Declaration of a variable in C associate it with a data type which defines the size, how it's value should be stored in memory, the range of values that can be stored and the set of valid operations for that variable.

Syntax for Declaring a Variable
data_type variable_name;
For Example     float sum;

In the above statement we are declaring some memory space for a variable called sum, to store a floating point data.

Multiple variables of same data type can be declared by separating the variable names by comma.
data_type variable_one, variable_two, variable_three;
For Example     int sum, amount, interest;

We can initialize a variable with some value at the time of declaration. The initialization statement consists of an equal sign followed by a constant after variable name.

data_type variable_name = value;
For Example     int sum = 0;
    float rate = 5.5, amount = 345.52;

Difference between Variable Declaration and Definition in C

  • Declaration of a variable declares the name and type of the variable whereas definition causes storage to be allocated for the variable.
  • There can be more than one declaration of the same variable but there can be only one definition for the variable.
  • In most cases, variable declaration and definition are same. However you can declare a variable without defining it by preceding a variable name with extern specifier.

Local Variable in C

A local variable in C is declared inside a function.

  • A local variable is visible only inside their function, only statements inside function can access that local variable.
  • Local variables are declared when control enters a function and local variables gets destroyed, when control exits from function.
#include <stdio.h>

int getSum(int var1, int var2){
    /*
     * Variable sum is local variable of getSum function 
     * and can only be accessed from within getSum function
     */   
    int sum = var1 + var2;
    return sum;
}

int main(){
   /* Variable a and b are local variables of main function.
    * Only statements within main function can access a and b
    * getSum function cannot access variable a and b   
    */
   int a = 5, b = 10;
   printf("Sum = %d", getSum(a, b));
   
   return 0;
}
In above program, variable a and b are local variables of main function similarly var1 and var2 are local variables of getSum. Variables a and b are only accessible from main function.

Output
Sum = 15

Global Variable in C

A variable that is declared outside of all functions is called global variable.

  • A global variable is visible to any every function and can be used by any piece of code.
  • Unlike local variable, global variables retain their values between function calls and throughout the program execution.
  • Global variables are declared outside of any function.
#include <stdio.h>
/* 
Sum is a global variable, which can be accessed from any function
*/
int sum;

void getSum(int var1, int var2){ 
    sum = var1 + var2;    
}

int main(){
   int a = 5, b = 10;
   getSum(a, b);
   /* Accessing global variable from main*/
   printf("Sum = %d", sum);
   
   return 0;
}
In above program, variable sum is a global variable because it is declared outside of any function. Variable sum can be accessed from any function.

Output
Sum = 15