C Introduction

C language 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 the DEC PDP-11 computer, and is one of the most influential languages in the modern programming language landscape.

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 C language specification for several years thereafter.

The UNIX operating system, the C compiler, and almost all UNIX applications are written in C. With its concise syntax, hardware-level control capabilities, and excellent runtime efficiency, C has become the most important professional language in system 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 highly efficient machine code, with execution speed close to assembly language.
  • It can directly manipulate memory and hardware, handling low-level tasks.
  • Highly portable, can be compiled and run on a variety of computer platforms and operating systems.
  • It has a rich standard library, a mature ecosystem, and a large community.

About C

  • C language was originally invented for writing the UNIX operating system, 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 working on a C language standard, and officially released C89 (also called ANSI C) in 1989. This was the first official standard for C.
  • By 1973, the UNIX operating system kernel had been completely rewritten in C, marking C's foundational position in system programming.
  • C is currently the most widely used system programming language, holding a core position for a long time in embedded development, operating systems, compilers, and other fields.
  • Many important foundational software and frameworks (including the CPython interpreter, the underlying components of the V8 engine, etc.) are implemented in C.
  • The most popular Linux operating system kernel and the core parts of the relational database management system MySQL are both written in C.
  • C has evolved through multiple standard versions such as C89, C99, C11, and C17, continuing to maintain vitality and modernity.

Why use C?

C was originally used for system development, especially for the core programs that make up operating systems. Since the machine code produced by compiling C runs at nearly the same speed as assembly language, while also offering better readability and maintainability than assembly, it has become the preferred language for system-level development. Below are several typical application scenarios for C:

  • Operating system kernels (e.g., Linux, Windows NT kernel, macOS XNU kernel)
  • Language compilers and interpreters (e.g., GCC, Clang, CPython)
  • Assembler and linker
  • 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, PostgreSQL)
  • High-performance computing and scientific computing libraries (such as BLAS, LAPACK)
  • Game engine internals and graphics rendering libraries (such as OpenGL implementations)

C Program

A C language program can be only a few lines or as many as millions of lines. The source code is stored in files with the extension".c"in a text file, for examplehello.c. For large projects, the code is usually split across multiple".c"source files and".h"header files, and compilation is uniformly managed by the build system.

You can use"vi"、"vim"、"VS Code"or any other text editor to write C language programs. After writing, compile the source code into an executable file using compilers 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 how to write source code in program files.


C11

C11 (also known as C1X) is the third major official standard of C, corresponding to the ISO standard ISO/IEC 9899:2011, officially released in December 2011. Based on C99, it further refined language features, with a focus on enhancing multithreading support, generic programming, and safety.

New Features

  • Standardization of alignment handling (Alignment): introducing_Alignasspecifiers andalignofoperators, andaligned_alloc()functions and<stdalign.h>header file, allowing programmers to precisely control the alignment of data in memory, which is especially important for performance-sensitive low-level development.

  • _Noreturnfunction specifier: 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 keywords: Allows selecting different code paths at compile time based on the type of an expression; it is the fundamental mechanism for implementing type-safe generic macros in C, making up for C's long-standing lack of generic capabilities.

  • Multithreading support: Introduced native multithreading support at the C language standard level for the first time, including:

    • _Thread_localThread-local storage type specifier, where each thread has its own independent copy of variables.
    • <threads.h>header file, providing thread creation (thrd_create), synchronization (mtx_lock/cnd_wait) and other management functions.
    • _Atomictype qualifiers and<stdatomic.h>header file, providing lock-free atomic operations, which is an important foundation for multithreaded programming.
  • Enhanced Unicode support: based on ISO/IEC TR 19769:2004, addedchar16_tandchar32_tdata types, corresponding to UTF-16 and UTF-32 encodings respectively, and provide<uchar.h>header file contains Unicode string conversion functions, improving C's support for internationalized programming.

  • Removegets()Function: due togets()does not check buffer boundaries, and has historically been a common source of buffer overflow vulnerabilities. C11 formally removed it from the standard, recommending the saferfgets()or newly addedgets_s()replacement.

  • Bounds-checking function interfaces (Annex K): defines a series of_ssafe functions with the suffix, such asfopen_s()、strcat_s()、strcpy_s()etc. These functions require the caller to pass in the buffer size, preventing buffer overflow problems at the root.

  • more floating-point handling macros: expanded<math.h>and<fenv.h>The floating-point handling macros provide more refined floating-point operation control and exception handling capabilities, meeting the needs of high-precision scientific computing.

  • Anonymous struct / union support: Allows nested definition of unnamed structures or unions within a structure or union. When accessing their members, no intermediate layer name is needed, simplifying the definition of complex data structures. This feature had long existed as an extension in GCC, and C11 incorporated it into the standard.

  • static assertion_Static_assert(): performs assertion checks on constant expressions at compile time; if the condition is not met, a compilation error is produced (rather than a runtime error). It is often 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"), indicating exclusive creation — if the file already exists, it fails, similar to that in POSIXO_CREAT|O_EXCLflag, often used in safe temporary file creation scenarios.

  • addedquick_exit()Function: Provides a third way to terminate a program (the other two arereturnandexit()), whenexit()when cleanup cannot be completed normally,quick_exit()will execute throughat_quick_exit()terminates immediately after the registered handler function, suitable for emergency exit scenarios in multithreaded programs.

other extensions