Variables Tutorials
Programming
Variables in C
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.
Why Do We Need Variables?
Variables help us:
Store data in memory.
Access data using meaningful names.
Update values during program execution.
Perform calculations and operations on stored data.
Declaring Variables
Before using a variable, it must be declared with a data type.
Syntax
data_type variable_name;Examples
int age;
float height;
char grade;Variable Declaration and Initialization
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';Complete Example
#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;
}Output
Age: 20
Height: 5.7
Grade: ARules for Naming Variables
A 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.
Valid Variable Names
age
studentName
_totalMarks
salary2025Invalid Variable Names
1age
student name
int
return
user-nameVariable Initialization
Initialization means assigning the first value to a variable.
Initialization During Declaration
int age = 20;
float height = 5.7;Initialization After Declaration
char grade;
grade = 'A';Note
If a local variable is not initialized, it contains a garbage value.
Accessing Variables
We can access a variable using its name.
Example
#include <stdio.h>
int main() {
int num = 3;
printf("%d", num);
return 0;
}Output
3Updating Variable Values
The value stored in a variable can be changed at any time using the assignment operator (=).
Example
#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;
}Output
Initial Value: 10
Updated Value: 25
After Adding 5: 30Using Variables in Expressions
Variables can be used in calculations and expressions.
Example
#include <stdio.h>
int main() {
int a = 20;
int b = 40;
int sum = a + b + 10;
printf("%d", sum);
return 0;
}Output
70Multiple Variable Declaration
Multiple variables of the same data type can be declared in a single statement.
Example
int a, b, c;
float x, y;Initialization
int a = 10, b = 20, c = 30;Memory Allocation for Variables
When a variable is defined, memory is allocated based on its data type.
Common Variable Sizes
Data TypeTypical Sizechar1 byteint4 bytesfloat4 bytesdouble8 bytes
Finding Variable Size
#include <stdio.h>
int main() {
int num = 22;
printf("%zu bytes", sizeof(num));
return 0;
}Output
4 bytesKey Points
A 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.