Variables in C (original) (raw)

Last Updated : 17 Oct, 2025

A variable in C is a named piece of memory which is used to store data and access it whenever required.

#include <stdio.h>

int main() { // integer variable int age = 20;

// floating-point variable
float height = 5.7;

// character variable
char grade = 'A';

printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);

return 0;

}

`

Output

Age: 20 Height: 5.7 Grade: A

**Rules for Naming Variables in C

We can assign any name to a C variable as long as it follows the following rules:

C Variable Initialization

#include <stdio.h>

int main() { // 1. Initialization at the time of declaration int age = 20; float height = 5.7;

// 2. Initialization after declaration
char grade;
grade = 'A';

printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);

return 0;

}

`

Output

Age: 20 Height: 5.7 Grade: A

**Note: It is compulsory that the values assigned to the variables should be of the same data type as specified in the declaration.

Accessing Variables

The data stored inside a C variable can be easily accessed by using the variable's name.

C `

#include <stdio.h>

int main() {

// Create integer variable
int num = 3;

// Access the value stored in
// variable
printf("%d", num);
return 0;

}

`

Changing Stored Values

We can also update the value of a variable with a new value whenever needed by using the assignment operator =.

C `

#include <stdio.h>

int main() {

// initial value
int number = 10;
printf("Initial value: %d\n", number);

// updating value
number = 25;
printf("Updated value: %d\n", number);

// updating again using expression
number = number + 5;
printf("After adding 5: %d\n", number);

return 0;

}

`

Output

Initial value: 10 Updated value: 25 After adding 5: 30

How to use variables in C?

Variables act as name for memory locations that stores some value. It is valid to use the variable wherever it is valid to use its value. It means that a variable name can be used anywhere as a substitute in place of the value it stores.

C `

#include <stdio.h> int main() {

// Expression that uses values
int sum1 = 20 + 40;

// Defining variables
int a = 20, b = 40;

// Expression that uses variables
int sum2 = a + b;

printf("%d\n%d", sum1, sum2);
return 0;

}

`

Memory Allocation of C Variables

When a variable is **declared, the compiler is told that the variable with the given name and type exists in the program. But no memory is allocated to it yet. Memory is allocated when the variable is **defined.

Most programming languages like C generally declare and define a variable in the single step. For example, in the above part where we create a variable, variable is declared and defined in a single statement.

The size of memory assigned for variables depends on the type of variable. We can check the size of the variables using **sizeof operator****.**

C `

#include <stdio.h>

int main() { int num = 22;

// Finding size of num
printf("%d bytes", sizeof(num));
return 0;

}

`

Variables are also stored in different parts of the memory based on their **storage classes****.**

Variables in C

Variable Naming Rules