What is a variable in C? Learn int, double, and char with clear diagrams.
What is a Variable — A Named Place to Store Data
A variable is a named area of storage that holds data. Variable name: the name of the storage area. Variable value: the data stored there.
When you need the data, you simply refer to it by name.
variable a
?
+
variable b
?
=
variable c
?
Where Variables Live — Inside the Computer
The values of variables are stored in the computer's memory (RAM).
CPU (Processor)
Controls the program and performs calculations.
Memory (RAM)
Holds data used by the CPU. Variables are stored here.
Hard Disk (Storage)
Long-term data storage.
Inside Memory — The World of Bits (0/1)
Memory is a sequence of bits that can hold the values 0 or 1. A variable's value is recorded in several consecutive bits, and the variable name labels that region.
Binary representation of int num = 5; in memory (32 bits)
Data Types — Every Variable Has a Type
Variables have types, and each type reserves a different number of bytes in memory.
Integer type: int
Example: -2, -1, 0, 1, 2, 3, ... No fractional part.
Reserves 4 bytes. int num = 5;
Floating-point: double
Example: 1.2, -2.68, 3.14, 79.69, ... For calculations with decimals.
Reserves 8 bytes. double pi = 3.14;
Size Comparison
char (1B)
int (4B)
float (4B)
double (8B)
Choose the type by "box shape"
Pick a type based on the values you want to store. Use this flowchart when unsure:
β Do you need decimals?
γβ Yes: double (more precise than float)
γβ No: go to β‘ β‘ Storing a single character?
γβ Yes: char
γβ No: go to β’ β’ Integers
γβ Values up to Β±2,147,483,647: int (use this by default)
γβ Larger: long long
Type
Typical use
Approximate range
int
counts, indices, scores
β -2.1B to +2.1B
long long
populations, timestamps, big IDs
β Β±9.2 Γ 1018
double
heights, averages, probabilities
15β17 significant digits
char
one ASCII char
-128 to 127
unsigned int
non-negative sizes
0 to 4.2B
Rule of thumb for beginners: use int for whole numbers, double for decimals. You rarely need to pick float, short, or long yourself.
Common beginner pitfalls
Pitfall 1: integer divided by integer truncates
The #1 surprise for C beginners:
int a = 7;
int b = 2;
double r = a / b; // r is 3.0 (not 3.5!)printf("%f\n", r); // β 3.000000
Why? int / int returns an int (truncated). 7/2 = 3, then 3 becomes 3.0 when stored into double.
// Fix: cast one operand to doubledouble r = (double)a / b; // β 3.5// Or declare as double from the startdouble a = 7, b = 2;
double r = a / b; // β 3.5
Pitfall 2: overflow past int's limit
int only holds about Β±2.1 billion. Japan's population fits; the world's does not.
int big = 2000000000;
big = big + 2000000000; // should be 4B...printf("%d\n", big); // β -294967296 (negative!)
This is overflow. For larger numbers use long long (%lld).
Pitfall 3: wrong format specifier
printf format must match the argument type, otherwise you see garbage or trigger undefined behavior.
Type
Format
Example
int
%d
printf("%d", 42);
double
%f / %lf
printf("%f", 3.14);
char
%c
printf("%c", 'A');
char * (string)
%s
printf("%s", "hello");
long long
%lld
printf("%lld", 9000000000LL);
Pitfall 4: uninitialized variables
int x; // declaration only; contents undefinedprintf("%d\n", x); // β garbage value (changes each run)// Correct:int x = 0; // initialize to 0
Always initialize: unlike Python, C doesn't auto-zero your variables.
Pitfall 5: naming rules
Letters, digits, and _ only (no kana/kanji)
Cannot start with a digit (1st bad, first good)
Reserved words can't be names (int, if, return, ...)
Case-sensitive: age and Age are different variables
Use meaningful names: score beats a, radius beats x1
Declaration, Assignment, and Overwriting
To use a variable, you first declare it (reserve storage in memory), then assign a value to it.
int num; // Declaration: reserve memory for the variable
num = 5; // Assignment: store a value ( = is "assign", NOT equality!)
Note:= is not the equals sign; it is assignment. It stores the right-hand value into the variable on the left.
Assigning to an undeclared variable is an error:x = 23; → "What is x??"
How Overwriting Works
Assigning a new value to a variable overwrites the previous value, which is lost. A variable can hold only one value at a time.
variable num
—
Assigning One Variable to Another
Writing x = y; copies the value of y into x (y itself does not change).
variable x
10
←
variable y
33
Step Execution — Watch Variables Change
variable_demo.c
Variable state
Name
Type
Value
Standard output
Try It Yourself — Variables
my_vars.c
Output
Click "Run" to execute...
π‘ Try these ideas too
Store a negative number like -100 in an int and print with %d