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

Lesson 3: Variables

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
TypeTypical useApproximate range
intcounts, indices, scoresβ‰ˆ -2.1B to +2.1B
long longpopulations, timestamps, big IDsβ‰ˆ Β±9.2 Γ— 1018
doubleheights, averages, probabilities15–17 significant digits
charone ASCII char-128 to 127
unsigned intnon-negative sizes0 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 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: 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.
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 auto-zero your variables.

Pitfall 5: naming rules

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

NameTypeValue

Standard output

 

Try It Yourself — Variables

my_vars.c
Output
Click "Run" to execute...
πŸ’‘ Try these ideas too
Advertisement

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

The int type stores integers. For decimals, 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 must start with a letter or underscore. Names starting with a digit, or reserved words like int, are not 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), while float is single-precision (~7 digits). double is typically preferred.

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