C Introduction
C is a general-purpose high-level programming language, originally designed by Dennis Ritchie at Bell Labs for developing the UNIX operating system. C was first implemented in 1972 on a DEC PDP-11 computer and is one of the most influential languages in the history of modern programming.
In 1978, Brian Kernighan and Dennis Ritchie co-published the book "The C Programming Language", which gave the first complete public description of C. This version became known as the K&R standard and served as the de facto specification of the language for years to come.
The UNIX operating system, the C compiler, and nearly all UNIX applications were written in C. With its concise syntax, hardware-level control, and excellent runtime efficiency, C has become the most important professional language in systems programming and has profoundly influenced the design of many successor languages such as C++, Java, Go, and Rust.
- Concise syntax, easy to learn and master.
- A structured programming language with clear logic.
- Compiles to efficient machine code, with runtime speed close to assembly language.
- Can directly manipulate memory and hardware to handle low-level tasks.
- Highly portable; can be compiled and run on a wide variety of computer platforms and operating systems.
- Rich standard library, mature ecosystem, and a large community.

About C
- C was originally invented to write the UNIX operating system and was born at Bell Labs in 1972.
- C evolved from the B language, which was designed by Ken Thompson around 1970.
- In 1983, the American National Standards Institute (ANSI) began work on standardizing C, and in 1989 officially released C89 (also known as ANSI C), the first official standard for the language.
- By 1973, the UNIX kernel had been completely rewritten in C, marking C's foundational role in systems programming.
- C is currently the most widely used systems programming language, holding a central position in embedded development, operating systems, compilers, and other fields.
- Many important pieces of foundational software and frameworks (including the CPython interpreter and low-level components of the V8 engine) are implemented in C.
- The core parts of today's most popular Linux kernel and the relational database management system MySQL are written in C.
- C has evolved through multiple standard versions including C89, C99, C11, and C17, continually maintaining its vitality and modernity.
Why use C?
C was originally used for systems development, especially for the core programs that make up an operating system. Since C compiles to machine code that runs almost as fast as assembly, while offering better readability and maintainability than assembly, it became the language of choice for system-level development. Here are a few typical application scenarios for C:
- Operating system kernels (such as Linux, the Windows NT kernel, and the macOS XNU kernel)
- Language compilers and interpreters (such as GCC, Clang, and CPython)
- Assemblers and linkers
- Text editors and command-line tools
- Printer firmware and drivers
- Network protocol stacks and network drivers
- Embedded systems and microcontroller development
- Database systems (such as SQLite, MySQL, and PostgreSQL)
- High-performance computing and scientific computing libraries (such as BLAS and LAPACK)
- Game engine internals and graphics rendering libraries (such as OpenGL implementations)
C Program
A C program can be just a few lines or as many as several million lines; the source code is stored in a text file with the extension ".c" , for example, hello.cFor large projects, the code is usually split into multiple ".c" source files and ".h" header files, with compilation managed uniformly by a build system.
You can use the "vi", "vim", "VS Code" or any other text editor to write C programs. Once written, the source code is compiled into an executable by a compiler such as GCC or Clang:
gcc hello.c -o hello # 编译
./hello # 运行(Linux/macOS)
This tutorial assumes you already know how to edit text files and write source code in program files.
C11
C11 (also known as C1X) is the third major official standard of C, corresponding to ISO/IEC 9899:2011, officially released in December 2011. Building on C99, it further refined the language, with particular emphasis on multithreading support, generic programming, and safety.
New Features
-
Standardization of alignment handling: introduces
_Alignasthe specifier andalignofoperator, and thealigned_alloc()function and theheader, allowing programmers to precisely control how data is aligned in memory, which is especially important for performance-sensitive low-level development. -
_NoreturnFunction specifiers: used to mark functions that never return (such asexit(),abort()), similar to GCC's__attribute__((noreturn)), helping the compiler perform better optimization and warning analysis. -
_GenericGeneric selection keyword: allows different code paths to be selected at compile time based on the type of an expression; it is the underlying mechanism for type-safe generic macros in C, filling a long-standing gap in the language's generic programming capabilities. -
Multithreading support: for the first time, native multithreading support was introduced at the C standard level, including:
_Thread_localThread-local storage type specifier, where each thread has its own independent copy of the variable.header, providing thread creation (thrd_create), synchronization (mtx_lock/cnd_wait), and other management functions._Atomictype qualifier andheader, providing lock-free atomic operations, an important foundation for multithreaded programming.
-
Enhanced Unicode support: based on ISO/IEC TR 19769:2004, adds
char16_tandchar32_tdata types corresponding to UTF-16 and UTF-32 encodings respectively, and providesa header containing Unicode string conversion functions, improving C's support for internationalized programming. -
Removes the
gets()function: Becausegets()does not check buffer boundaries and has historically been a common source of buffer overflow vulnerabilities; C11 officially removed it from the standard, recommending the saferfgets()or the newly addedgets_s()as a replacement. -
bounds-checking function interfaces (Annex K): defines a series of safe functions with
_ssuffixes, such asfopen_s(),strcat_s(),strcpy_s(), etc. These functions require the caller to pass the buffer size, preventing buffer overflow problems at the source. -
More floating-point handling macros: extends the
andfloating-point macros in, providing finer control over floating-point operations and exception handling to meet the needs of high-precision scientific computing. -
Anonymous struct/union support: allows unnamed structs or unions to be defined nested within a struct or union, with no intermediate name needed to access their members, simplifying the definition of complex data structures. This feature had long existed in GCC as an extension, and C11 incorporated it into the standard.
-
Static assertions
_Static_assert(): asserts constant expressions at compile time; if the condition is not met, a compile error (rather than a runtime error) is produced. It is commonly used to verify compile-time assumptions such as type sizes and struct offsets, improving code robustness. -
fopen()New exclusive mode"x": append to the file open mode string"x"(such as"wx") to indicate exclusive creation, which fails if the file already exists, similar to theO_CREAT|O_EXCLflag in POSIX, commonly used for secure temporary file creation. -
New
quick_exit()function: provides a third way to terminate a program (the other two arereturnandexit()); whenexit()cannot complete cleanup normally,quick_exit()executes the handlers registered viaat_quick_exit()and then terminates immediately, suitable for emergency exit scenarios in multithreaded programs.