πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ 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)

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...
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