A variable is a named memory location used to store data in a C program. The value stored in a variable can be used in calculations, conditions, functions, and other operations.
A variable is a named storage location that holds a value. For example:
int age = 20;
Here, age is a variable that stores the value 20.
Variables allow us to:
A variable can be declared by specifying its data type followed by its name.
int age;
This declares an integer variable named age.
Initialization means assigning an initial value to a variable when it is declared.
int age = 20;
Here, age is initialized with the value 20.
A variable can first be declared and then assigned a value.
int age;
age = 20;
Both declaration and assignment are separate statements in this example.
The data type tells the compiler what kind of value a variable is intended to store.
int age = 20;
float price = 99.50;
char grade = 'A';
Different data types are suitable for different kinds of values.
The int type is commonly used for integer values.
int age = 25;
int marks = 85;
These variables store whole-number values.
The float type can store floating-point values.
float price = 99.50f;
The f suffix can be used to indicate a float
literal.
The char type is used to store a single character.
char grade = 'A';
A character constant is normally written inside single quotation marks.
The double type is used for floating-point values with greater
precision than float.
double salary = 25000.75;
Common rules for variable names include:
Examples of valid variable names:
age
studentName
marks1
_total
course_fee
Examples of invalid variable names:
1age
student-name
total fee
int
These names violate one or more C identifier rules.
C treats uppercase and lowercase letters as different.
int age = 20;
int Age = 30;
Here, age and Age are two different variables.
A variable's value can be changed after declaration.
int age = 20;
age = 25;
After the assignment, age contains 25.
Variables can be displayed using printf().
#include <stdio.h>
int main()
{
int age = 20;
printf("%d", age);
return 0;
}
The %d conversion specifier is commonly used with an
int value in printf().
int marks = 85;
printf("%d", marks);
A floating-point value can be printed with %f.
float price = 99.50f;
printf("%f", price);
The %c conversion specifier is used to print a character.
char grade = 'A';
printf("%c", grade);
Multiple variables can be declared in a program.
int age = 20;
float height = 5.8f;
char grade = 'A';
Each variable can store a different type of value.
Variables of the same data type can be declared together.
int a = 10, b = 20, c = 30;
This creates three integer variables.
Variables are frequently used in mathematical calculations.
int a = 10;
int b = 20;
int sum = a + b;
printf("%d", sum);
The value of sum becomes 30.
The assignment operator = is used to assign a value to a variable.
int marks;
marks = 90;
The value on the right side is assigned to the variable on the left side.
One variable can receive the value of another variable.
int a = 10;
int b;
b = a;
After the assignment, b contains the value 10.
A variable declared inside a function or block is generally a local variable. Its scope is limited to the block where it is declared.
int main()
{
int age = 20;
printf("%d", age);
return 0;
}
A variable declared outside all functions has file scope and is commonly called a global variable.
#include <stdio.h>
int count = 10;
int main()
{
printf("%d", count);
return 0;
}
| Variable | Constant |
|---|---|
| Value can generally be changed | Value is intended not to be changed through the declared constant |
int age = 20; |
const int age = 20; |
Use meaningful names that describe the stored value.
int studentAge;
float courseFee;
int totalMarks;
Meaningful names make programs easier to read.
#include <stdio.h>
int main()
{
int age = 20;
int marks = 85;
float fee = 5000.0f;
char grade = 'A';
printf("Age = %d\n", age);
printf("Marks = %d\n", marks);
printf("Fee = %.2f\n", fee);
printf("Grade = %c\n", grade);
return 0;
}
Create a program that stores and displays a student's name, age, marks, and grade.
#include <stdio.h>
int main()
{
int age = 18;
int marks = 90;
char grade = 'A';
printf("Age = %d\n", age);
printf("Marks = %d\n", marks);
printf("Grade = %c\n", grade);
return 0;
}
Practice by changing the variable values and observing the output.
= assigns a value to a variable.int, float, double, and char
are common C data types.Question: Which symbol is used to assign a value to a variable in C?