Nearby lessons

18 of 124

C - 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)

RuleValidInvalid
Start with a letter or underscore_total, sum9marks (starts with digit)
No spaces or special symbols insidetotal_markstotal marks, a#b
Cannot be a keywordstudentint, if, for
C is case sensitivesum, 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
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int age; // declaration - make a box named age
6 age = 20; // assignment - put 20 inside
7 printf("Age is %d\n", age); // 20
8 
9 age = 25; // change the value
10 printf("Now age is %d\n", age); // 25
11}
Output

Age is 20
Now age is 25
      
📝 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 Questions
Progress: 0 / 2