Variables in C

Variables in C are used to store data that can be manipulated during program execution. Each variable in C must be declared with a specific data type. This ensures the variable only stores values of that type.

Definition

A variable is a named location in memory used to hold a value. The value of a variable can change during the execution of the program.

Syntax:


data_type variable_name;
                
                

Example:

                        
#include <stdio.h>
int main() 
{
  int num = 10;
  float pi = 3.14;
  char ch = 'A';
          
  printf("Integer: %d\n", num);
  printf("Floating point: %.2f\n", pi);
  printf("Character: %c\n", ch);
          
  return 0;
}
                        
                    

Output

Integer: 10
Floating point: 3.14
Character: A

Variable Types

C supports various types of variables based on the data they store:

Example:

                
#include <stdio.h>
int main() 
{
 int age;
 float height;
 char grade;
        
 age = 20;
 height = 5.8;
 grade = 'A';
        
 printf("Age: %d\n", age);
 printf("Height: %.1f\n", height);
 printf("Grade: %c\n", grade);
        
 return 0;
}
                
            

Output

Age: 20
Height: 5.8
Grade: A

The three components of declaring a variable

Let us explain the three aspects of defining a variable: variable declaration, variable definition, and variable initialization, along with examples

1. Variable Declaration:

The process of telling the compiler about a variable's existence and data type is known as variable declaration. It notifies the compiler that a variable with a specific name and data type will be used in the program. Still, no memory for the variable is allocated at this moment. It is usually seen at the start of a function or block before the variable is utilized.

The general syntax for variable declaration is

Syntax


data_type variable_name;  
           
            

Example of variable declaration:

Example

    
#include <stdio.h>
int main() 
{  
    // Variable declaration  
    int age;  
    float salary;  
    char initial;  
      
    return 0;  
}  

    

2. Variable Definition:

The process of reserving memory space for the variable to keep its contents during program execution is known as a variable definition. It is based on the data type and connects the variable name with a particular memory address of sufficient size. A variable in C can be declared and defined in the same statement, although they can also be separated if necessary.

Example

        
#include <stdio.h>
int main() 
{  
   // Variable definition  
    int age = 25;  
    float salary = 2500.5;  
    char initial = 'J';  
      
    return 0;  
}  
    
        
    

3. Variable Initialization:

Variable declaration is the act of informing the compiler about the existence and data type of a variable. It informs the compiler that a variable with a specific name and data type will be used in the program, but that memory for the variable still needs to be allocated.

Example of variable initialization:

        
#include <stdio.h>
int main() 
{  
    // Variable definition and initialization  
    int age = 25;  
    float salary = 2500.5;  
    char initial = 'J';  
    // Later in the program, you can change the value of the variable  
    age = 30;  
    salary = 3000.0;  
      
    return 0;  
}