Variables in C Language
Variables in C Language
A variable is a name given to a memory location. Variables are used to access and modify data stored in memory. During program execution, the value stored in a variable can change, which is why it is called a variable.
Key Aspects of Variables in C Language
-
Name:
A unique identifier used to refer to data stored in memory -
Value:
The actual data stored in that memory location -
Type:
A classification that determines what kind of data a variable can store
Example:
int quantity = 10;
Here
- quantity → variable name
- 10 → value stored in the variable
- int → variable type (can store integer data)
Variable Representation in Memory
- Variable Name: quantity
- Data Type: int
- Memory Allocation: a box representing memory
- Value: 10
Data Types in C Language
A data type specifies what kind of data a variable can store.
Data types help the computer system handle data correctly and efficiently.
Basic Data Types in C Language
-
int:
Whole numbers (10, 200, -30) -
float:
Decimal numbers (3.25, 100.49, -2.89) -
double:
Large or more precise decimal numbers (2763.9876652) -
char:
A single character (‘a’, ‘D’, ‘3’)
Variable Declaration in C Language
Syntax:
data_type variable_name;
Example:
int quantity;
float price;
char letter;
Here:
- data_type: Determines what type of data the variable can store (int, float, char, double).
- variable_name: The name given to the variable (identifier).
- quantity: An int type variable named quantity is created.
- price: A float type variable named price is created.
- letter: A char type variable named letter is created.
Declaring More Than One Variable at a Time in C Language
Example:
int a, b, c, d;
Here, int type variables named a, b, c, and d are created.
Variable Initialization in C Language
A variable can be assigned a value at the time of declaration
or after declaring the variable.
Syntax:
data_type name = value;
Example 1:
int marks = 60;
Here, the value is assigned at the time of declaration.
Example 2:
int marks;
marks = 60;
Here, the variable is declared first and assigned a value later.
Here:
- data_type: Determines the type of data the variable can store.
- name: The variable name (identifier).
- value: The value stored in the variable.
- marks: An int type variable named marks is created and the value 60 is stored in it.
Example Program: Creating Variables in C Language
#include<stdio.h>
int main() {
int a = 10;
int b;
b = 20;
printf("Value of a = %d", a);
printf("Value of b = %d", b);
return 0;
}
In the above program, two variables named a and b are created.
The value of variable a is assigned at the time of declaration.
The value of variable b is assigned after declaration.