What is a variable in C? Learn int, double, and char with clear diagrams.
π What to learn on this page
β Must-know essentials
int for integers, double for decimals, char for one character
Declare: int x;, assign: x = 5;
= is assignment, not equality
Always initialize before use (int x = 0;)
β Read if you have time
Type sizes are implementation-defined (know typical values)
int / int truncates β classic beginner trap
Signed integer overflow is undefined behavior
What is a Variable — A Named Place to Store Data
A variable is a named slot of storage that holds a piece of data.
The variable name labels the slot; the value is whatever's stored inside.
When you need the data later, you just refer to it by name.
variable a
?
+
variable b
?
=
variable c
?
Where Variables Live — Inside the Computer
Variable values are stored in the computer's memory (RAM).
CPU (Processor)
Runs the program and does the calculations.
Memory (RAM)
Holds the data the CPU is working with. Variables live here.
Hard Disk (Storage)
Long-term data storage.
Inside Memory — The World of Bits (0/1)
Memory is a long sequence of bits, each holding a 0 or a 1. A variable's value occupies a few 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
Every variable has a type, and each type reserves a different number of bytes in memory.
Integer type: int
Examples: -2, -1, 0, 1, 2, 3, ... No fractional part. Typically 4 bytes (implementation-defined). int num = 5;
Floating-point: double
Examples: 1.2, -2.68, 3.14, 79.69, ... For numbers with a decimal part. Typically 8 bytes (IEEE 754 double). double pi = 3.14;
Size Comparison (typical values)
char (1B)
int (4B)
float (4B)
double (8B)
Important: the C standard only fixes minimum widths (e.g. int β₯ 16 bits, long β₯ 32 bits). Actual sizes are implementation-defined. The numbers above are typical on modern desktop/Linux/macOS with gcc. Print sizeof(int) via %zu to check your own environment.
Pick a type by "box shape"
Choose the type based on the values you need to hold. When in doubt, run through this flowchart:
β 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 on 32-bit int
long long
populations, timestamps, big IDs
at least 64 bits, β Β±9.2 Γ 1018
double
heights, averages, probabilities
15β17 significant digits (IEEE 754)
char
one ASCII char
typically -128 to 127 (signedness is implementation-defined)
unsigned int
non-negative sizes
0 to β4.2B on 32-bit int
Rule of thumb for beginners:int for whole numbers, double for decimals. You rarely need to reach for float, short, or long yourself.
Common beginner pitfalls
Pitfall 1: int divided by int truncates
One of the most common gotchas 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 (the fractional part is discarded). 7/2 is 3, and that 3 is then stored as 3.0 when it goes into a 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: overflowing int's range
A 32-bit int only holds about Β±2.1 billion. Japan's population fits; the world's doesn't.
int big = 2000000000;
big = big + 2000000000; // should be 4B...printf("%d\n", big); // result is undefined (you often see a negative number on real hardware)
Important: in the C standard, signed integer overflow is undefined behavior. Don't memorize it as "wraps around to negative." Compilers assume overflow never happens when they optimize, so the result can be genuinely unpredictable β disappearing loops, bizarre results, you name it.
By contrast, unsigned overflow is defined to wrap modulo 2N.
Safer options: β widen to long long (%lld); β‘ check against INT_MAX (from <limits.h>) before the operation; β’ enable runtime detection with -fsanitize=undefined on gcc/clang.
Pitfall 3: wrong format specifier
A printf format specifier has to match the argument's type. Mismatch and you'll see garbage output β or worse, 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 zero out your variables for you.
Pitfall 5: naming rules
Letters, digits, and _ only β no other symbols
Can't start with a digit (1st is invalid, first is fine)
Reserved words aren't allowed as names (int, if, return, ...)
Case-sensitive: age and Age are two different variables
Pick meaningful names: score beats a, and radius beats x1
Declaration, Assignment, and Overwriting
Before you can use a variable, you declare it (which reserves storage in memory), then assign a value to it.
int num; // Declaration: reserve memory for the variable
num = 5; // Assignment: store a value ( = means "assign", NOT equality!)
Heads up:= isn't the equals sign from math class β it's assignment. It copies the value on the right into the variable on the left.
Assigning to an undeclared variable is an error:x = 23; → "what's x?!"
How Overwriting Works
Assigning a new value to a variable overwrites the old one β the previous value is gone. A variable holds exactly one value at a time.
variable num
—
Assigning One Variable to Another
x = y; copies the value of y into x. y itself stays the same.
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...
π‘ A few things to try
Store a negative number like -100 in an int and print it with %d
Put 3000000000 in an int and watch it overflow
Compare double x = 7 / 2; with double x = 7.0 / 2;