πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Lesson 3: Variables

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
TypeTypical useApproximate range
intcounts, indices, scoresβ‰ˆ -2.1B to +2.1B on 32-bit int
long longpopulations, timestamps, big IDsat least 64 bits, β‰ˆ Β±9.2 Γ— 1018
doubleheights, averages, probabilities15–17 significant digits (IEEE 754)
charone ASCII chartypically -128 to 127 (signedness is implementation-defined)
unsigned intnon-negative sizes0 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 double
double r = (double)a / b;   // β†’ 3.5
// Or declare as double from the start
double 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.
TypeFormatExample
int%dprintf("%d", 42);
double%f / %lfprintf("%f", 3.14);
char%cprintf("%c", 'A');
char * (string)%sprintf("%s", "hello");
long long%lldprintf("%lld", 9000000000LL);

Pitfall 4: uninitialized variables

int x;               // declaration only; contents undefined
printf("%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

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

NameTypeValue

Standard output

 

Try It Yourself — Variables

my_vars.c
Output
Click "Run" to execute...
πŸ’‘ A few things to try

Related Lessons

Getting Started
Lesson 2: Hello World
Write your first C program and learn the compile and run flow.
Getting Started
Lesson 4: printf & scanf
How to use printf and scanf in C. A complete reference of format specifiers.
Reference
C Cheat Sheet
Quick reference for printf, operators, types, and more.
← Previous lesson
Lesson 2: Hello World
Next lesson →
Lesson 4: printf & scanf

Review Quiz

Check your understanding of this lesson!

Q1. What can an int variable hold?

Decimal numbers
Integers
Strings

int stores integers. For decimal numbers use float or double; for strings use a char array.

Q2. Which of these is a valid variable name?

2value
my_var
int

Variable names have to start with a letter or underscore. Names that start with a digit, or use reserved words like int, aren't allowed.

Q3. What is the difference between double and float?

double has higher precision
float has higher precision
No difference

double is double-precision floating point (~15 digits), and float is single-precision (~7 digits). double is usually the better default.

Share this article
Share on X (Twitter) Share on Facebook