A variable is something that stores a value in memory and allows it to be called by name. Data type determines what type of value and how many bytes are stored in the box.
Learning Objectives
- Separate Declaration/initialization/assignment variables.
- Distinguish between the size and use of C's basic data types (
int,float,double,char). - Check the data type size with the
sizeofoperator. - Define constants as
constand#define.
Why are there multiple data types?
A large box may seem like a good idea if it can hold value, but in reality, that's not the case.
- If you store the integer 100 in 8 bytes
long, 7 bytes are wasted. - If 3 billion (about 3.1 billion) is stored in 4 bytes
int, overflow will occur. - 3.14 can never be expressed with
int.
The data type determines “what type of value this memory slot holds” and allows the compiler to determine. It determines the Arithmetic/Input/Output/Storage method.
Key concepts
1) Lifetime of a variable
int age; //declaration only (value is garbage value)
age = 20; //college admissions
int year = 2026; //Initialized upon declaration
In terms of memory:
Immediately after declaration, after substitution
┌──────────────┐ ┌──────────────┐
│ age : ??? │ │ age : 20 │
└──────────────┘ └──────────────┘
2) Basic data type (based on 64-bit Linux)
| data type | size | Expression range (approximately) |
|---|---|---|
char |
1 byte | -128 ~ 127 |
short |
2 byte | -32,768 ~ 32,767 |
int |
4 bytes | -2.1 billion ~ +2.1 billion |
long |
8 bytes | very large |
float |
4 bytes | Approximately 7 digit precision |
double |
8 bytes | Approximately 15 digit precision |
Adding unsigned gives up the negative range and doubles the positive range.
3) Constants: Naming unchanging values
#define PI 3.14159 //Preprocessor macros — replace text before compilation
const int MAX = 100; //Normal variable but cannot be changed
Instead of scattering literals (3.14159) throughout the code, put the name in one place.
If the value changes, you only need to change one part of the definition.
4) sizeof operator
It tells you how many bytes a data type or variable takes up. It may vary depending on the operating system and compiler, so printing it directly is useful for debugging.
printf("%zu\n", sizeof(int)); //Normal 4
See as example
Example 1 — ex01_variables.c: Declaration → Assignment → Update
int age = 20;
int year;
year = 2026;
printf("Age: %d years old\n", age);
printf("Year: Year %d\n", year);
age = age + 1;
printf("Age next year: %d years old\n", age);
Execution result
Age: 20
Year: 2026
Age next year: 21
Key point: = is not “equal to” but “substitute the value on the right into the left”.
Example 2 — ex02_types.c: Output format for each data type
int score = 95;
float ratio = 0.75f;
double price = 19999.99;
char grade = 'A';
Execution result
Integer (%d): 95
Real (%f): 0.750000
Real (%.2f): 0.75
Double precision (%lf): 19999.990000
Character (%c): A
ASCII of characters (%d): 65
Key point: The character ('A') is also output as integer code (65). That is, char is a small integer.
Example 3 — ex03_sizeof.c: Check data type size directly
Execution result
char = 1 byte
short = 2 byte
int = 4 byte
long = 8 byte
float = 4 byte
double = 8 byte
Size of variable a: 4 bytes
Key point: The result of sizeof is of type size_t, so printf is output as %zu.
Example 4 — ex04_const.c: Two ways of a constant
Execution result
Circumference (macro): 3.141590
Maximum users (macro): 100
Week (const): 7 days
Area of a circle with radius 5.0: 78.539750
Key point: const undergoes data type checking like a variable, and #define is a simple text substitution.
A different perspective — “Labeled Boxes”
variable = labeled memory box
Data type = size and shape of the box
┌─────────┐ ┌─────────┐ ┌─────────────┐
│ age │ │ score │ │ price │
│ int │ │ int │ │ double │
│ 4 byte │ │ 4 byte │ │ 8 byte │
│ [20] │ │ [95] │ │ [19999.99] │
└─────────┘ └─────────┘ └─────────────┘
If you try to put a different shape value in the same box (assigning 3.14 to int), the compiler
Cut off the decimal part (truncation) and insert the value.
Common Mistakes
- Use of uninitialized variable:
int x; printf("%d", x);→ Output garbage value. - Integer overflow:
int n = 3000000000;→ It may be negative due to exceeding the expression range. - Floating point error: Even if you add 0.1 100 times with
float, it will not be exactly 10. %dvs%fconfusion: If the data type and format do not match, garbage is output.'A'vs"A"confusion: Single quotation marks are characters (1 byte), double quotation marks are strings (2 bytes,'A'+'\0').
Cleanup
- A variable is a name given to memory, and a data type is the size of that memory and how it is interpreted.
- In 64-bit Linux,
int=4 anddouble=8 bytes are default. - Give constants meaningful names to reduce magic numbers.
- Let’s get into the habit of checking the size directly with
sizeof. - Data types and format specifiers (
%d,%f, etc.) must match.
Try it yourself
cd src
gcc -std=c11 -Wall -o ex01 ex01_variables.c && ./ex01
gcc -std=c11 -Wall -o ex02 ex02_types.c && ./ex02
gcc -std=c11 -Wall -o ex03 ex03_sizeof.c && ./ex03
gcc -std=c11 -Wall -o ex04 ex04_const.c && ./ex04
Application:
- Try printing by adding
long longandunsigned inttoex03_sizeof.c. - Uncomment
DAYS_IN_WEEK = 8;ofex04_const.cand check what error the compiler generates.