
We have already learned aboutshort and int types. A short typically occupies 2 bytes, while an int usually occupies 4 bytes. The larger the byte size, the greater the range of data it can represent. For example, the int type can represent values around ±2 billion, as clearly stated in Microsoft’s documentation.
Today, we will discuss thelong type, which is stated in Microsoft’s documentation to “occupy 4 bytes.” Wait, what is the difference from int? If both are 4 bytes, why do we need a long type? Don’t worry, this is precisely the exciting part of our lesson today! The story of long, and later long long, begins with the CPU architecture.
01
Unveiling CPU Architecture: From 16-bit, 32-bit to 64-bit
To understand the “unpredictability” of the long type, we first need to grasp a core concept at the hardware level—CPU architecture, or more straightforwardly, the “bitness” of the CPU. The earliest CPUs, such as the Intel 8080 from the 1970s, the Most Technology 6502 (the chip used in Apple I and Apple II), and the Z80, were all merely 8-bit processors. This means they could only process 8 bits of data at a time, with very limited processing power, and the games played were mostly simple pixel-level graphics.
Subsequently,16-bit processors gradually became popular. Then came a critical milestone—Intel launched the8086 processor, which was a groundbreaking32-bit processor! At that time, while other manufacturers were still lingering in the 8-bit and 16-bit realm, the emergence of the 32-bit processor was undoubtedly a “nuclear bomb” in the computer world. To commemorate this historic breakthrough, Intel gave it a codename—X86 architecture.
Key point! X86 architecture thus became synonymous with 32-bit CPU architecture.
Later, with technological advancements,64-bit processors emerged. Intel named itX86-64, which is what we commonly refer to today asX64 architecture. Therefore, when we talk about X86 architecture, we refer to 32-bit CPUs, while X64 architecture refers to 64-bit CPUs.
This also explains why we often see software having “32-bit installation packages” and “64-bit installation packages,” such as Photoshop, CAD, etc. They are designed to adapt to different CPU architectures and operating systems.
02
Operating Systems and Memory: The “Ceiling” of 32-bit
Operating systems also come in 32-bit and 64-bit versions. Before Windows 10, this distinction was particularly evident. A very important concept is:
-
A 32-bit operating system can support a maximum of about 4GB of memory. To be precise, the actual memory it can recognize and use is usually slightly less than 4GB (around 3.x GB).
-
A 64-bit operating system can support more than 4GB of memory, even larger amounts.
This means that if your early computer had 4GB or more memory but was running a 32-bit operating system, then part of the memory would “disappear” and could not be utilized by the system. Therefore, to fully utilize large memory, you must install a 64-bit operating system and use a 64-bit CPU.
Professional Correction and Supplement: Common Misconceptions about void main()
In a previous video, the example of void main() was mentioned. Here, it needs to be corrected and deepened: According to the standard C language specification, the main function should return an int type value, indicating the program’s exit status. While void main() may pass in some compilers, it is not a standard practice and is not recommended for use in formal projects. The standard declaration of the main function should be int main() or int main(int argc, char *argv[]).
// Example: Standard C language main function declarationint main() { // Program body return 0; // 0 usually indicates successful execution}int main(int argc, char *argv[]) { // Main function with command line arguments // argc: number of arguments // argv: array of argument strings return 0;}
03
The Mystery of long Type: CPU Architecture Determines Size
Now, we can return to the puzzle of the long type! Why does Microsoft’s documentation say it is 4 bytes, while you might hear it could be 8 bytes? The answer lies in the CPU architecture we just discussed:
-
In a 32-bit CPU architecture (X86 architecture), the long type occupies 4 bytes. At this point, it is the same size as the int type.
-
In a 64-bit CPU architecture (X64 architecture), the long type occupies 8 bytes. At this point, its storage range is twice that of int, allowing it to handle larger data.
This explains why the long type seems to have “no difference”—because at certain historical periods and architectures, it indeed was the same size as int! Microsoft’s documentation focuses more on the 32-bit environment, which makes it seem somewhat “outdated,” but it is not incorrect.
So, when you write C language code now, if you compile on a 64-bit system, long will typically be 8 bytes.
04
long long: The Everlasting 8-byte Giant
Since long varies with CPU architecture, is there a type that has a fixed size and can represent a vast range of data? The answer is **long long**!
The long long type was introduced in the C99 standard, and it is guaranteed to occupy at least 8 bytes (usually 8 bytes). Whether you are on a 32-bit or 64-bit system, long long will provide you with immense data storage capacity, capable of handling values up to ±9 quintillion. When you need to handle integers larger than what long can manage on a 32-bit system, long long is your best choice.
#include <stdio.h>#include <limits.h> // Include macros like INT_MAX, LONG_MAX, etc.int main() { printf("Size of short: %zu bytes\n", sizeof(short)); printf("Size of int: %zu bytes\n", sizeof(int)); printf("Size of long: %zu bytes\n", sizeof(long)); printf("Size of long long: %zu bytes\n", sizeof(long long)); printf("INT_MAX: %d\n", INT_MAX); printf("LONG_MAX: %ld\n", LONG_MAX); printf("LLONG_MAX: %lld\n", LLONG_MAX); return 0;}
Run this code on your system to visually see the actual sizes and maximum values of long and long long.
05
Summary: The Co-evolution of long and CPU Architecture
In today’s lesson, we not only learned about the long type in C language but, more importantly, we understood the historical process closely related to CPU architecture and the development of operating systems.
-
The 32-bit X86 architecture and 64-bit X64 architecture are key to understanding the changes in the sizes of these data types.
-
The memory limitations of 32-bit systems (around 4GB) drove the proliferation of 64-bit systems.
-
The size of the long type varies between CPU architectures at 4 bytes and 8 bytes, while long long consistently occupies 8 bytes, providing a larger data range.
-
Backward compatibility is an important factor to consider in software design, but it can also lead to adaptation challenges (e.g., running old games on modern systems).
