Module 2 · Variables, Types, Input and Operators — topic 1 of 6
Variables
A variable is a named piece of memory with a type. Learn how to declare, initialise, assign and name variables, and why an uninitialised variable is dangerous in C.
Learning objectives
- Declare and initialise variables of different types
- Distinguish declaration, initialisation and assignment
- Follow C's identifier rules and naming conventions
Definition
A variable is an object — a region of storage — identified by a name, whose type determines its size, the values it can hold and the operations allowed on it.
Introduction
Programs need to remember things: a running total, a user's age, the number of attempts left. In C you create a variable by stating its type and its name, for example int age;. The compiler reserves enough memory for an int and lets you refer to it as age.
Unlike Python, C does not give new variables a default value. A local variable that you declare but do not initialise contains whatever bits were already in that memory.
Why it matters
Reading an uninitialised local variable is undefined behaviour: the program may print garbage, crash, or appear to work until it is graded. Always initialising variables removes an entire category of bugs.
Choosing the right type for each variable decides how much memory it uses and what values are valid — an essential skill when you later work with arrays of millions of elements or with hardware registers.
Detailed explanation
A declaration introduces a name and type: int count;. An initialisation gives a value at the moment of creation: int count = 0;. An assignment changes the value later: count = 5;.
Several variables of one type can be declared together: int x = 1, y = 2, z; — here only z is uninitialised.
Identifier rules: names contain letters, digits and underscores, must not start with a digit, are case-sensitive (total and Total differ) and cannot be keywords such as int or return. Names beginning with an underscore followed by a capital letter, or with two underscores, are reserved for the implementation.
Variables declared inside a function are local (automatic): they exist only while the function runs. Variables declared outside every function are global and are initialised to zero automatically. Scope is covered in detail in the storage-classes lesson.
Undefined behaviour
Using an uninitialised local variable does not raise an error in C. The compiler may assume it never happens and generate surprising code. Treat -Wall's “may be used uninitialized” warning as an error.
Syntax
- The type comes first, then the name.
=in a declaration initialises; elsewhere it assigns.- Since C99 you may declare variables anywhere in a block, not only at the top.
Code examples
Declare, initialise, assign
Fruit: 10 Apples now: 5
Different types
Roll 21, section B, CGPA 8.75
More examples
Swapping two variables
Before: a=10 b=25 After: a=25 b=10
Step-by-step explanation
int a = 10, b = 25;declares and initialises both variables in one statement.int temp = a;saves the original value ofa(10) before it is overwritten.a = b;copies 25 intoa; at this moment both hold 25.b = temp;restores the saved 10 intob, completing the swap.
Expected output
Running “Swapping two variables” prints exactly:
Practical applications
- Sorting algorithms swap array elements exactly like this thousands of times.
- Embedded code uses variables mapped to hardware registers to control LEDs and motors.
- Game loops keep the player's position, health and score in variables updated every frame.
Best practices
- Initialise every variable where you declare it.
- Use descriptive lower-case names such as
total_marksortotalMarksand stay consistent. - Declare variables close to where they are first used.
Common mistakes
Using a variable before giving it a value
Avoid
Prefer
total starts with an indeterminate value, so the result is garbage. Initialise accumulators to 0.
Starting a name with a digit
Avoid
Prefer
Identifiers cannot begin with a digit; the compiler reports a syntax error.
Important notes
- Global and
staticvariables are zero-initialised; local (automatic) variables are not. - C23 makes
true,falseandboolkeywords, so avoid those as variable names even in older code.
Key takeaways
- A variable has a type, a name and a value stored in memory.
- Declaration, initialisation and assignment are different operations.
- Uninitialised locals hold indeterminate values — always initialise.
Try it yourself
- 1Declare variables for a student's roll number, name initial, and percentage, initialise them, and print them on one line.
- 2Swap two integers without a temporary variable using addition and subtraction, then explain why the temporary-variable method is safer.
Show hint
Think about overflow when the numbers are large.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the value of an uninitialised local `int` variable in C?
- BeginnerWhich is a valid C identifier?
- BeginnerWhat is printed?