Watch your variables Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

A variable = a name + a type + a value

A variable is a named place in memory. Its type tells C how many bytes to reserve and how to read them: the same 4 bytes mean something different as an int or as a float. Declare a variable before you use it, and give it a value straight away.

type  name   = value;

int    age   = 19;       // whole number, 4 bytes
double price = 49.99;    // decimal number, 8 bytes
char   grade = 'A';      // one character, 1 byte
const int DAYS = 7;      // read-only
The basic types (typical sizes on today's 64-bit computers)
Type Size Holds Range / precision printf Example
char 1 byte one character (a small number) -128 to 127 %c 'A'
unsigned char 1 byte small non-negative number 0 to 255 %d 200
short 2 bytes whole number -32,768 to 32,767 %hd 1200
int 4 bytes whole number about ±2.1 billion %d 19
long long 8 bytes big whole number about ±9.2 × 10¹⁸ %lld 8000000000LL
float 4 bytes decimal number about 7 significant digits %f 49.99f
double 8 bytes decimal number about 15–16 significant digits %f (%lf in scanf) 3.14159

Declaring variables

  1. Write the type, the name, and (best) a first value: int count = 0;
  2. Names use letters, digits and _ and cannot start with a digit: total_2 is fine, 2total is not.
  3. C is case-sensitive: age and Age are two different variables.
  4. Keywords such as int, return or while cannot be used as names.
  5. A variable without a first value holds garbage — whatever was in that memory before.

Sizes, ranges and overflow

  1. Each type has a fixed size in bytes; sizeof(type) tells you how many.
  2. 1 byte = 8 bits, so it can hold 2⁸ = 256 different values.
  3. unsigned types hold only 0 and up; signed types split the range around 0.
  4. Going past the range does not stop the program: the value wraps around.
  5. float and double store most decimals only approximately (0.1 is not exact).

Conversions and constants

  1. int / int gives an int: 7 / 2 is 3. The decimals are dropped, not rounded.
  2. If one side is a double, the other is converted and the result is a double.
  3. A cast converts on purpose: (double) a / b gives 3.5.
  4. Storing a double in an int cuts off the decimal part: 78.9 becomes 78.
  5. const makes a variable read-only; #define PI 3.14159 is the older way.
Walkthrough

Average of three marks

Every line, explained.

1 #include <stdio.h> Brings in printf.
2
3 int main(void) The program starts here.
4 {
5 const int SUBJECTS = 3; A read-only int. Using a name instead of the number 3 makes the code clearer and easy to change.
6 int maths = 78, science = 85, english = 90; Three int variables declared on one line, each with a first value.
7 int total = maths + science + english; int + int + int is an int: 253.
8 double average = (double) total / SUBJECTS; The cast makes total a double first, so the division keeps its decimals: 84.333… Without it the result would be 84.0.
9 char grade = 'A'; A char holds one character, written in single quotes. Inside it is the number 65.
10 printf("Total: %d\n", total); %d prints an int: Total: 253
11 printf("Average: %.2f\n", average); %.2f prints a double with 2 decimals: Average: 84.33
12 printf("Grade: %c\n", grade); %c prints a char: Grade: A
13 return 0; Success.
14 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Using a variable before giving it a value

✗ Wrong
int total;
total = total + 5;
✓ Right
int total = 0;
total = total + 5;

A variable without a first value contains garbage left in memory, so the result is unpredictable.

Expecting decimals from int / int

✗ Wrong
int a = 7, b = 2;
double avg = a / b;   // 3.0
✓ Right
int a = 7, b = 2;
double avg = (double) a / b;   // 3.5

The division happens first, with two ints, so the decimals are gone before the value reaches the double.

The wrong format specifier

✗ Wrong
double price = 49.99;
printf("%d\n", price);
✓ Right
double price = 49.99;
printf("%.2f\n", price);

printf trusts the specifier: %d reads the bytes of a double as an int and prints nonsense. Use %f for float and double.

Choosing a type that is too small

✗ Wrong
int population = 8000000000;
✓ Right
long long population = 8000000000LL;

8 billion is bigger than an int can hold (about 2.1 billion). It overflows — use long long for very big whole numbers.

Comparing decimals with ==

✗ Wrong
double x = 0.1 + 0.2;
if (x == 0.3) ...   // false!
✓ Right
double x = 0.1 + 0.2;
if (fabs(x - 0.3) < 1e-9) ...

0.1 and 0.2 are stored approximately, so the sum is 0.30000000000000004. Compare decimals with a small tolerance (fabs is in math.h).

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

How many bytes does a char take?

Question 2

What is the value of x after: int x = 9 / 4;

Question 3

An unsigned char holds 255. What does it hold after adding 1?

Question 4

Which line gives d the value 3.5?

Question 5

What happens if your program tries to change a const variable?

Assessment

Variables & Data Types quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.