Loading content...
Loading content...
A variable is a named memory location used to store data in a program. It allows us to store, access, and modify values whenever needed.
Instead of remembering memory addresses, we use variable names to work with data easily.
Variables help us:
Store data in memory.
Access data using meaningful names.
Update values during program execution.
Perform calculations and operations on stored data.
Before using a variable, it must be declared with a data type.
data_type variable_name;int age;
float height;
char grade;We can declare a variable and assign a value to it at the same time.
int age = 20;
float height = 5.7;
char grade = 'A';#include <stdio.h>
int main() {
int age = 20;
float height = 5.7;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}Age: 20
Height: 5.7
Grade: AA variable name must follow these rules:
Can contain letters, digits, and underscores (_).
Must start with a letter or underscore.
Cannot start with a digit.
Cannot contain spaces.
Cannot be a C keyword.
Must be unique within its scope.
age
studentName
_totalMarks
salary20251age
student name
int
return
user-nameInitialization means assigning the first value to a variable.
int age = 20;
float height = 5.7;char grade;
grade = 'A';If a local variable is not initialized, it contains a garbage value.
We can access a variable using its name.
#include <stdio.h>
int main() {
int num = 3;
printf("%d", num);
return 0;
}3The value stored in a variable can be changed at any time using the assignment operator (=).
#include <stdio.h>
int main() {
int number = 10;
printf("Initial Value: %d\n", number);
number = 25;
printf("Updated Value: %d\n", number);
number = number + 5;
printf("After Adding 5: %d\n", number);
return 0;
}Initial Value: 10
Updated Value: 25
After Adding 5: 30Variables can be used in calculations and expressions.
#include <stdio.h>
int main() {
int a = 20;
int b = 40;
int sum = a + b + 10;
printf("%d", sum);
return 0;
}70Multiple variables of the same data type can be declared in a single statement.
int a, b, c;
float x, y;int a = 10, b = 20, c = 30;When a variable is defined, memory is allocated based on its data type.
Data TypeTypical Sizechar1 byteint4 bytesfloat4 bytesdouble8 bytes
#include <stdio.h>
int main() {
int num = 22;
printf("%zu bytes", sizeof(num));
return 0;
}4 bytesA variable is a named memory location.
Variables are used to store and manage data.
Every variable must have a data type.
Variables should be initialized before use.
Variable values can be updated during program execution.
Variables can be used in expressions and calculations.
Memory allocation depends on the variable's data type.
The sizeof() operator is used to find the memory size of a variable.