Explain the variable declaration, initialization and assignment in C language

In C programming, variables are named storage locations in memory that hold data values. Unlike constants, variables can have their values modified during program execution. Before using any variable, it must be declared to inform the compiler about its data type and name.

Syntax

data_type variable_name;
data_type variable_name = initial_value;
data_type var1, var2, var3;

Variable Declaration

Variable declaration tells the compiler to reserve memory space for a variable with a specific data type. The syntax is −

type variable_name;

or for multiple variables of the same type −

type variable_name1, variable_name2, variable_name3;

For example −

#include <stdio.h>

int main() {
    int a, b;        /* Declare two integer variables */
    float c;         /* Declare a float variable */
    double d;        /* Declare a double variable */
    char letter;     /* Declare a character variable */
    
    printf("Variables declared successfully<br>");
    return 0;
}
Variables declared successfully

Variable Initialization

Variable initialization means assigning an initial value to a variable at the time of declaration. The syntax is −

data_type variable_name = initial_value;

Here's an example showing initialization −

#include <stdio.h>

int main() {
    int width = 10, height = 20;    /* Initialize multiple variables */
    char letter = 'R';              /* Initialize character variable */
    float area = 26.5;              /* Initialize float variable */
    double pi = 3.14159;            /* Initialize double variable */
    
    printf("width: %d, height: %d<br>", width, height);
    printf("letter: %c<br>", letter);
    printf("area: %.1f<br>", area);
    printf("pi: %.5f<br>", pi);
    
    return 0;
}
width: 10, height: 20
letter: R
area: 26.5
pi: 3.14159

Variable Assignment

Variable assignment is the process of giving a value to an already declared variable using the assignment operator (=). This can be done after declaration −

#include <stdio.h>

int main() {
    int height, base;    /* Declaration */
    float result;
    
    /* Assignment */
    height = 40;
    base = 31;
    result = height * base;
    
    printf("Height: %d<br>", height);
    printf("Base: %d<br>", base);
    printf("Result: %.2f<br>", result);
    
    return 0;
}
Height: 40
Base: 31
Result: 1240.00

Rules for Variable Names

  • Variable names can contain alphabets, digits, and underscores
  • Variable names must start with an alphabet or underscore (not a digit)
  • No whitespace is allowed in variable names
  • Variable names cannot be C keywords (int, return, if, etc.)
  • Variable names are case-sensitive (age and Age are different)

Complete Example

Here's a comprehensive example demonstrating declaration, initialization, and assignment −

#include <stdio.h>

int main() {
    /* Variable declaration */
    int a, b;
    int c;
    float f;
    
    /* Variable assignment */
    a = 40;
    b = 50;
    c = a + b;
    f = 3.14;
    
    /* Variable initialization (declaration + assignment) */
    int initialized_var = 100;
    
    printf("Value of a: %d<br>", a);
    printf("Value of b: %d<br>", b);
    printf("Value of c: %d<br>", c);
    printf("Value of f: %.2f<br>", f);
    printf("Initialized variable: %d<br>", initialized_var);
    
    return 0;
}
Value of a: 40
Value of b: 50
Value of c: 90
Value of f: 3.14
Initialized variable: 100

Conclusion

Variable declaration reserves memory, initialization assigns initial values during declaration, and assignment gives values to already declared variables. Understanding these concepts is fundamental for effective C programming and memory management.

Updated on: 2026-03-15T13:16:25+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements