Variables, and the boxes they name
A declaration reserves memory and fixes what can legally go in it. Choosing the wrong type is not a style mistake — it silently changes your answers.
After this lesson
You should be able to
- Declare variables of type int, double, and char and describe what each stores.
- Explain why int division discards the fractional part.
- Choose the right type for a given quantity and justify it.
A declaration is a reservation
int marks; asks the compiler for a piece of memory big enough to hold a whole number and gives that piece the name marks. From then on, marks is how you reach that memory. The type is not a label you can ignore — it decides how many bytes are reserved and how the bits inside are interpreted.
A declared variable does not start at zero. Until you assign to it, it holds whatever was left in that memory by an earlier program. Reading it before assigning gives an unpredictable value, and the answer may change between runs.
int marks; /* whole numbers: 0, 42, -7 */
double average; /* real numbers: 3.5, -0.25 */
char grade; /* one character: 'A', '?', '7' */
marks = 87;
average = 76.5;
grade = 'B';The division trap
If both operands of / are integers, C performs integer division and throws away the fractional part. 7 / 2 is 3, not 3.5. The result is not rounded — it is truncated toward zero. This is the single most common source of wrong marks in first-year programs.
Making just one operand a real number fixes it: 7 / 2.0 is 3.5. Storing an integer division into a double does not help, because the damage happens during the division, before the assignment.
int total = 7, count = 2;
double wrong, right;
wrong = total / count; /* 3.0 — divided as ints first */
right = (double) total / count; /* 3.5 — cast forces real division */char holds a number too
A char stores a single character, but underneath it is a small integer — the character's code. 'A' is stored as 65 on almost every machine you will meet. That is why 'B' - 'A' is 1, and why you can test grade >= 'A' && grade <= 'D'.
Single quotes and double quotes are different things. 'A' is one character. "A" is a string — two bytes, the A and a hidden terminator. Mixing them up produces confusing type errors.
Try it yourself
Three subjects score 78, 81 and 90. Compute and print the average correct to two decimal places, without changing the marks to double.
Need a hint?
The sum is an int and 3 is an int. What does that make the division?
Check the worked solution
The sum 249 divided by 3 happens to be exactly 83, so this particular case hides the bug. Change 90 to 91 and integer division would give 83 instead of 83.33. Casting makes the program correct for every input, not just the lucky one — which is the actual lesson.
#include <stdio.h>
int main(void)
{
int a = 78, b = 81, c = 90;
int sum = a + b + c;
double average;
average = (double) sum / 3;
printf("Average = %.2f\n", average);
return 0;
}Quick check
int a = 9, b = 4; double r = a / b; — what does r hold?
Why this lesson exists
Syllabus mapping
Variable Declarations and Data Types · C Language Elements
Maps to course outcomes CO2, CO3.