Variables in C
A program needs to remember things: your marks, a price, a name. It stores them in variables.
Think of a variable as a labelled box in the computer's memory. The label is the name, and the thing inside is the value. In this lesson you will learn how to create a variable, put a value in it, and change it.
What You Will Learn
- What a variable is.
- The difference between declaration and initialization.
- Rules for naming variables.
- How to change the value of a variable.
- Types of variables: local, global and static.
What is a Variable in C?
Exam definition: A variable is a named memory location used to store data. Its value can change while the program runs.
- Every variable has a name (like
marks), a data type (likeint) and a value (like90). - The data type decides what kind of value it can store and how much memory it uses.
- "Variable" means "can vary", that is, the value can change.
Memory trick: Variable = labelled box. Type = size of the box, name = label on the box, value = what is inside.
Declaration vs initialization
| Declaration | Initialization | |
|---|---|---|
| Meaning | Creating the variable (making the box) | Giving it its first value (filling the box) |
| Example | int marks; | int marks = 90; |
| Memory | Memory is reserved | Memory is reserved and a value is stored |
A local variable that is declared but not initialized holds a garbage value: some random leftover number. Always give a value before you use it.
Rules for naming variables
Variable names follow the identifier rules you learned earlier:
- Only letters, digits and underscore
_. - Must start with a letter or underscore.
- No spaces, no special characters, no keywords.
- Case-sensitive:
totalandTotalare different.
Types of variables (by where they are declared)
| Type | Declared | Used |
|---|---|---|
| Local | Inside a function or block | Only inside that function or block |
| Global | Outside all functions | Anywhere in the program |
| Static | With the static keyword | Keeps its value between function calls |
You will learn these in detail in the lessons on scope and storage classes.
Syntax of Variable Declaration in C
data_type variable_name; /* declaration */
data_type variable_name = value; /* declaration + initialization */
data_type name1 = value1, name2, name3; /* many variables of the same type */
Examples: int age = 19;, float price = 49.5f;, char grade = 'A';, int a = 1, b = 2, c;
Example 1: Declare, Initialize and Change a Variable
#include <stdio.h>
int main(void)
{
int marks = 75; /* declare and initialize */
printf("Old marks: %d\n", marks);
marks = 82; /* change the value */
printf("New marks: %d\n", marks);
marks = marks + 5; /* add 5 to the current value */
printf("After bonus: %d\n", marks);
return 0;
}
Output
Old marks: 75 New marks: 82 After bonus: 87
Code Explanation
int marks = 75;: creates an integer variable namedmarksand stores 75 in it.printf("... %d\n", marks);:%dis replaced by the value ofmarks.marks = 82;: the old value 75 is replaced by 82. A variable holds only one value at a time.marks = marks + 5;: take the current value (82), add 5, and store the result (87) back inmarks.
How Variables Work in Memory
- When the program reaches
int marks, the computer reserves a block of memory (usually 4 bytes for anint). - That block has an address (a number). The name
marksis just an easy label for that address. - Writing
marks = 82stores 82 in that block, replacing whatever was there. - Reading
marksfetches the current value from that block.
Example 2: C Program to Calculate Simple Interest
#include <stdio.h>
int main(void)
{
float principal = 10000.0f; /* amount in rupees */
float rate = 7.5f; /* interest rate per year (%) */
int years = 2;
float interest = (principal * rate * years) / 100;
printf("Principal: %.2f\n", principal);
printf("Simple Interest: %.2f\n", interest);
printf("Total Amount: %.2f\n", principal + interest);
return 0;
}
Output
Principal: 10000.00 Simple Interest: 1500.00 Total Amount: 11500.00
Simple Interest = (P × R × T) / 100 = (10000 × 7.5 × 2) / 100 = 1500.
Important Points to Remember
- A variable is a named memory location.
- Every variable must be declared before it is used.
- Declaration creates it; initialization gives the first value.
- An uninitialized local variable has a garbage value.
- A variable holds one value at a time; a new value replaces the old one.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Using a variable before declaring it | Error: 'x' undeclared | Declare it first: int x; |
| Using a variable without a value | Garbage value in the output | Initialize it: int sum = 0; |
| Declaring the same name twice | Error: redefinition of 'x' | Declare once, then just assign |
90 = marks; | Error: lvalue required | The variable goes on the left: marks = 90; |
Variables in C Practice Questions
- What is a variable? Why is it called a variable?
- What is the difference between declaration and initialization?
- What is a garbage value?
- Write a statement to declare three integer variables
a,b,cwitha= 10. - What is the value of
xafter:int x = 5; x = x * 2; x = x - 3;?
Show answers
- A named memory location that stores data. It is called a variable because its value can change.
- Declaration creates the variable (
int a;). Initialization gives it its first value (int a = 5;). - A random leftover value in an uninitialized local variable.
int a = 10, b, c;- 7 (5 × 2 = 10, then 10 − 3 = 7).
Coding Exercise
Write a program that stores the length (12) and width (8) of a rectangle in variables, calculates its area and perimeter in two more variables, and prints both.
Show solution
#include <stdio.h>
int main(void)
{
int length = 12, width = 8;
int area = length * width;
int perimeter = 2 * (length + width);
printf("Area = %d\n", area);
printf("Perimeter = %d\n", perimeter);
return 0;
}
Area = 96 Perimeter = 40
Quick Quiz: Variables in C (MCQ)
-
A variable is a:
A) Fixed value B) Named memory location C) Keyword D) Function
-
Which is a valid declaration with initialization?
A)
int = 5 x;B)x int = 5;C)int x = 5;D)int 5 = x; -
An uninitialized local variable contains:
A) 0 B) 1 C) A garbage value D) An error
-
After
int a = 4; a = a + a;, the value ofais:A) 4 B) 8 C) 16 D) 44
-
A variable declared outside all functions is:
A) Local B) Global C) Static D) Constant
Show answers
- B: a named memory location.
- C:
int x = 5; - C: a garbage value.
- B: 8.
- B: global.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Variable | Named memory location; value can change |
|---|---|
| Parts | Data type + name + value |
| Declaration | int x; (creates it) |
| Initialization | int x = 10; (first value) |
| Uninitialized local | Garbage value |
| Types (by place) | Local, Global, Static |
Size of int | Usually 4 bytes |
Frequently asked questions
- What is the difference between declaration and initialization?
- Declaration creates the variable; initialization gives it its first value.
- What is a garbage value?
- A random leftover value in a local variable that was not initialized.
- Can a variable hold two values at once?
- No. A new value replaces the old one.
- Why must a variable be declared before use?
- So the compiler knows its type and how much memory to reserve.
Remember
- Variable = labelled box (type = size, name = label, value = inside).
- Always initialize before use:
int sum = 0;
Next Topic
Next lesson: Data Types in C. You will learn which types of data C can store and how much memory each one uses.