
In the last lesson, we explored the “behind-the-scenes story” of variable definitions in memory, and I believe everyone has gained a preliminary understanding of memory, hard drives, and the binary language of computers. Today, we will continue to delve deeper, uncovering the true mystery behind why an int type variable “occupies four bytes” and the range of values it can represent!
01
The principle of byte occupation for int type variables: Why is it 4 bytes?
In the last lesson, we mentioned that when defining int sunflower = 9673;, the program allocates a space in memory to store this value. So, how large is this space?
The answer is: on most 32-bit or 64-bit systems, an int type typically occupies 4 bytes (Byte).
A review of the relationship between bytes and bits:
Do you remember the movable type printing performance we discussed in the last lesson?
-
Memory can be imagined as a huge stage with countless small boxes.
-
Each box (or understood as a performer) represents a bit (bit), which can only represent two states: “standing up” (1) or “sitting down” (0).
-
1 byte = 8 bits. This means that one byte is like a small squad composed of 8 performers.
Therefore, if an int type occupies 4 bytes, it requires **4 bytes * 8 bits/byte = 32 bits** of storage space! This is like needing a “super squad” of 32 performers to represent an int value.
Why is it 4 bytes?
This is determined by the C language standard and different system architectures. The specific size of the int type is not fixed; it depends on the compiler and operating system platform you are using. However, in today’s mainstream 32-bit or 64-bit systems, the int type is typically designed to occupy 4 bytes to balance storage efficiency and the range of values it can represent. You can verify this using the built-in sizeof operator in C:
#include <stdio.h>
int main() {
printf("int type occupies %zu bytes\n", sizeof(int));
return 0;
}
Running this code will show you the number of bytes that the int type occupies on your current system.
02
How is the int value stored in memory?
When we define int sunflower = 1;, what happens in memory?
Allocating 4 bytes (32 bits) of space: The computer finds a continuous 4 bytes in memory.
Converting to binary: The decimal number 1 is converted to a 32-bit binary number. For 1, it is represented in memory as:
00000000 00000000 00000000 00000001
(Each “0” or “1” here is a “bit”)
These 32 zeros and ones, like the 32 performers in movable type printing, only the last performer “standing up” represents 1, while the other 31 performers are “sitting down” representing 0.
If we define int sunflower = 25;, then the 32-bit binary representation of 25 will be:
00000000 00000000 00000000 00011001
During program execution, it is like conducting this movable type printing performance, precisely manipulating each bit in memory to achieve data storage and computation. When you close the program, all these “performers” will “fall down”, the memory space is released, waiting for the next exciting performance.
03
The range of values that int type can represent: Positive and negative differences
Since the int type occupies 32 bits, what is the range of values it can represent?
Some may intuitively think of 2 to the power of 32, but there is a key point to consider: the int type needs to represent positive integers, negative integers, and zero simultaneously.
To distinguish between positive and negative, computers typically use the highest bit (the leftmost bit) among these 32 bits as the sign bit:
If the sign bit is 0, it indicates that this is a positive number or zero.
If the sign bit is 1, it indicates that this is a negative number.
As a result, the bits actually used to represent the magnitude of the value are only 32 – 1 = 31 bits.
Therefore, a 4-byte (32-bit) int type variable has a value range of:
-
Minimum value: -2 to the power of 31
-
Maximum value: 2 to the power of 31 – 1
Why subtract 1 from the maximum value?
This is because we also need to represent a 0. In the positive range, from 0 to 2 to the power of 31 – 1, there are a total of 2 to the power of 31 different values. If we do not subtract 1, we cannot represent 0.
For example, if we only have 3 bits to represent values (with 1 bit as the sign bit and the remaining 2 bits representing the value), then these 2 bits can represent 00, 01, 10, 11, which corresponds to decimal values 0, 1, 2, 3, totaling 2 to the power of 2 = 4 values. Thus, the maximum value is 2 to the power of 2 – 1 = 3.
Therefore, the specific range of the int type is approximately from -2,147,483,648 to 2,147,483,647. This range is usually sufficient for everyday integer calculations.
04
Summary
Through today’s learning, we have thoroughly understood the principle of why the int type occupies 4 bytes, how it is stored in memory, and the precise range of values it can represent. This seemingly tedious low-level knowledge is the foundation for writing efficient and correct C language programs.
Understanding how memory works is like mastering the “sign language” of computers, which can help us communicate better with machines. In the next lesson, we will continue to explore the performance of other data types in memory and further discuss how to avoid common issues such as integer overflow.
