The Design Philosophy of C Language
The design philosophy of C language can be summarized as “Trust the Programmer“.
Unlike many modern programming languages, C language imposes very few restrictions on the behavior of programmers, assuming that programmers know what they are doing.

Thus, C language is actually a language that demands a lot from programmers.
Decades have passed, and despite the emergence of many new programming languages, C language remains the dominant language for operating system and device driver development. This is not a coincidence, but rather a perfect match between the features of C language and the needs of system programming, one of the key factors being that C language can achieve direct control over hardware.
How is this achieved?
CPU Registers and Memory
Before understanding how C language can directly control hardware, we need to first understand two core components of computer hardware: CPU registers and physical memory.

These two components form the basis for executing instructions and storing data in a computer, and they are the key interfaces through which C language can achieve low-level control.
CPU registers are high-speed, small-capacity storage units within the processor, and they are the direct objects of operation when the CPU executes instructions.
Registers can be thought of as the “workbench” of the CPU, where all calculations and data processing must take place.
Whether loading instructions, performing calculations, or accessing memory, the involvement of registers is indispensable.

The main functions of registers include:
- Storing temporary data during instruction execution
- Saving memory addresses for memory access
- Recording the working state of the CPU (e.g., whether the result of a calculation is zero, whether a carry has occurred, etc.)
- Controlling the flow of program execution (e.g., the address of the next instruction)
Next, let’s look at physical memory.
Physical memory, usually referring to the main storage (RAM, Random Access Memory), is the primary storage device used by the computer to store program code, data, and runtime information. If we compare registers to the “workbench” of the CPU, then physical memory is the “large warehouse” of the computer, storing all the data needed for program execution.
The main functions of physical memory include:
- Storing the currently executing program code
- Saving data during program execution (e.g., variables, arrays, structures, etc.)
- Maintaining the running state of the program (e.g., function call stack, heap memory, etc.)

When we say that C language can directly control hardware, it is more reflected in the control over registers and memory.
The Tool for C Language to Control Registers: Inline Assembly
Inline assembly allows embedding assembly instructions directly within C code, enabling low-level operations that cannot be expressed in C syntax:
-
Directly read and write specific CPU registers: Accessing specific registers like EAX, CR0, etc.
-
Execute privileged instructions: Operations that require special permissions, such as modifying page tables or changing processor modes.
-
Optimize for extreme performance: Using hand-optimized assembly code in performance-critical paths, etc.
The GCC compiler provides powerful support for inline assembly, with basic syntax as follows:
// Store the value of EAX register into result variable
asm volatile ("movl %%eax, %0" : "=r"(result) : );
// Load the value of variable value into EAX register
asm volatile ("movl %1, %%eax" : : "r"(value));
// Perform a system call
asm volatile ("int $0x80" : : "a"(syscall_num), "b"(arg1));
Inline assembly is the most direct manifestation of C language penetrating its own abstraction to reach hardware.
<span>asm</span> blocks can directly manipulate physical registers (EAX, EBX, etc.) or specific memory addresses, bypassing the variable abstraction of C language and the compiler’s register allocation mechanism.
The operating system kernel extensively uses inline assembly to implement:
- Context switching (saving and restoring register states)
- Switching processor privilege levels
- Page table operations
- Interrupt handling
- Atomic operations
Although inline assembly is powerful, it also brings risks and challenges:
- Breaking portability
- Increasing code complexity
- Potentially introducing hard-to-debug errors
Therefore, inline assembly is often regarded as a “last resort,” used only when absolutely necessary, and is typically encapsulated in macros or functions to improve maintainability.
The Tool for C Language to Control Memory: Pointers
Before understanding pointers in C language, we must grasp the essence of variables.
When we declare a variable in C language (e.g., <span>int a; char c;</span>), what are we actually doing?
Essentially, we are requesting a block of memory from the compiler and giving it a name and type. The compiler allocates an appropriate amount of memory based on the variable’s type and records the starting address of this memory.
For example, when we declare <span>int a;</span>, the compiler will:
- Allocate 4 bytes (on most modern systems) in the appropriate memory area (usually the stack)
- Associate this memory with the identifier
<span>a</span> - Record that this memory should be interpreted as an integer type

The variable name is a programmer-friendly identifier that exists only in the source code and compilation phase. Once the program is compiled into machine code, the variable name is replaced with a specific memory address. When the CPU executes instructions, it is unaware of the existence of variable names; it only knows to operate on data at specific memory addresses.
Essentially, a pointer is also a variable, but its value is the memory address of another variable; in other words, a pointer “points to” a location in memory.
For example, <span>int *p;</span> declares a pointer to an integer, indicating to the compiler that <span>p</span>‘s value is a memory address, and the data stored at that address should be interpreted as an integer.

Since pointers are also variables, they can be subjected to regular operations like addition and subtraction, allowing C language to directly manipulate memory addresses and achieve precise control over hardware.
It is important to note that in user mode, although pointers can be used, they operate on virtual memory, which is still not true physical memory. However, in kernel mode, the operating system can directly manipulate physical memory.
Through pointers, C language establishes a bridge between high-level language abstraction and low-level hardware operations.
The low-level control capabilities of C language make it an ideal tool for addressing these challenges, although this also means that programmers need to take on more responsibility to ensure the correctness and safety of the code.
In summary, when you use C language for system programming, you need to be very clear about what you are doing
Source: The Survival of a Programmer on a Deserted Island
Editor: Yue
The reproduced content only represents the author’s views
It does not represent the position of the Institute of Physics, Chinese Academy of Sciences
If reprinting is needed, pleasecontact the original public account
Recent Popular Articles Top 10
↓ Click the title to view ↓
1. Why Did Our Ancestors Plant a Bitter and White “Watermelon”?
2. Why Are High School Exam Papers Transported with Rifles While Cash is Transported with Shotguns? | Serious Play
3. You May Not Know Us, But You Have Definitely Eaten Us, and You Don’t Even Know We Are Toxic
4. What Happens When You Feed an Image to AI 500 Times? Even Beginners Can | Serious Play
5. The Expiration Date on Mineral Water Bottles is Actually for the Water? Can Water Expire?
6. How Many Lychees Can You Eat in a Day? Exceeding This Number May Cause Problems
7. A Mushroom That Can Be Drunk Just by Tearing Off the Lid? It Can Also Be Used as Hand Cream?
8. Can Parallel Lines Intersect? The Answer is Right Under Your Feet
9. Why is Our Heart Located on the Left Side? | No.465
10. The Statue of Liberty Was Originally Not Green? This Matter Must Be Asked to the Horseshoe Crab…
Click here to view all past popular articles
