Nearby lessons
18 of 124C - Variables
Variables is one of the foundational topics in C programming. This lesson explains What is a Variable?, Variables and Naming Rules and Program: declare, assign, change, print with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is a Variable?
A variable is a named memory location that can hold different values at different times. It is like a box with a label.
In simple words: a variable is a labelled box in memory. Declaration makes the box, assignment puts a value inside, and later you can empty it and put a new value in.
Variables and Naming Rules
The naming rules (with valid and invalid examples)
| Rule | Valid | Invalid |
|---|---|---|
| Start with a letter or underscore | _total, sum | 9marks (starts with digit) |
| No spaces or special symbols inside | total_marks | total marks, a#b |
| Cannot be a keyword | student | int, if, for |
| C is case sensitive | sum, Sum, SUM all different | — |
Program: declare, assign, change, print
Watch the full life of a variable — declare it, give it a value, change it, and print it at each stage:
Example03
📝 Key Takeaways
- A variable is a labelled box in memory that can hold different values.
- Declaration makes the box: int age; — assignment puts a value in: age = 20.
- Identifier rules: start with letter/underscore, no spaces/symbols, no keywords.
- C is case sensitive — sum, Sum and SUM are three different variables.
- You can change a variable's value any time: age = 25;
🧠 Test Your Knowledge
2 QuestionsProgress: 0 / 2