YouTip LogoYouTip

C Intro

C is a general-purpose high-level programming language, originally designed by Dennis Ritchie at Bell Labs for developing the UNIX operating system. First implemented in 1972 on a DEC PDP-11 computer, C is one of the most influential languages in the modern programming language ecosystem.

In 1978, Brian Kernighan and Dennis Ritchie jointly published the book "The C Programming Language," which provided the first complete public description of the C language. This version became known as the K&R standard and served as the de facto C language specification for many years thereafter.

The UNIX operating system, C compilers, and almost all UNIX applications are written in C. With its concise syntax, close-to-hardware control capabilities, and excellent runtime efficiency, C has become the most important professional language in the field of 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.
  • Structured programming language with clear logic.
  • Compiles to highly efficient machine code, with execution speed approaching that of assembly language.
  • Can directly manipulate memory and hardware, handling low-level tasks.
  • Highly portable, capable of being compiled and run on multiple computer platforms and operating systems.
  • Rich standard library, mature ecosystem, and large community.

Image 1

  • 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 working on a C language standard, officially releasing C89 (also known as ANSI C) in 1989, which was the first official standard for C.
  • By 1973, the UNIX operating system kernel had been completely rewritten in C, marking C's foundational status in systems programming.
  • C is currently the most widely used systems programming language, holding a core position in fields like embedded development, operating systems, and compilers for a long time.
  • Many important foundational software and frameworks (including the CPython interpreter, underlying components of the V8 engine, etc.) are implemented in C.
  • Today's most popular Linux operating system kernel and the core part of the relational database management system MySQL are written in C.
  • C has evolved through multiple standard versions like C89, C99, C11, and C17, continuously maintaining its vitality and modernity.

C was initially used for systems development, particularly for the core programs that constitute operating systems. Because the machine code generated by C compilers runs at a speed almost comparable to assembly language while offering better readability and maintainability than assembly, it has become the language of choice 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)
  • 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 (e.g., SQLite, MySQL, PostgreSQL)
  • High-performance computing and scientific computing libraries (e.g., BLAS, LAPACK)
  • Game engine internals and graphics rendering libraries (e.g., OpenGL implementations)

A C program can consist of just a few lines or up to millions of lines. The source code is saved in a text file with the extension ".c", for example, _hello.c_. For large projects, the code is usually split into multiple ".c" source files and ".h" header files, managed and compiled uniformly by a build system.

You can use "vi", "vim", "VS Code", or any other text editor to write C programs. After writing, compile the source code into an executable file using a compiler like GCC or Clang:

gcc hello.c -o hello # Compile
./hello # Run (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 for the C language, corresponding to the ISO standard ISO/IEC 9899:2011, officially released in December 2011. It further refined the language features based on C99, with a focus on enhancing multithreading support, generic programming, and security.

New Features

  • Standardized Alignment Handling: Introduced the `_Alignas` specifier and the `alignof` operator, along with the `aligned_alloc()` function and the `` header file, allowing programmers to precisely control data alignment in memory. This is particularly important for performance-sensitive low-level development.
  • `_Noreturn` Function Marker: Used to mark functions that never return (like `exit()`, `abort()`), similar to GCC's `__attribute__((noreturn))`, helping the compiler perform better optimization and warning analysis.
  • `_Generic` Generic Selection Keyword: 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, compensating for C's long-standing lack of generic capabilities.
  • Multithreading Support: For the first time, native multithreading support is introduced at the C language standard level, including:
    • `_Thread_local` thread-local storage type specifier, where each thread has its own independent copy of a variable.
    • The `` header file, providing management functions for thread creation (`thrd_create`), synchronization (`mtx_lock`/`cnd_wait`), etc.
    • The `_Atomic` type qualifier and the `` header file, providing lock-free atomic operations, which are a crucial foundation for multithreaded programming.
  • Enhanced Unicode Support: Based on ISO/IEC TR 19769:2004, new data types `char16_t` and `char32_t` were added, corresponding to UTF-16 and UTF-32 encodings respectively, along with the `` header file containing Unicode string conversion functions, improving C's support for internationalized programming.
  • Removal of the `gets()` Function: Because `gets()` does not check buffer boundaries and was a common source of buffer overflow vulnerabilities historically, C11 officially removed it from the standard, recommending the safer `fgets()` or the newly added `gets_s()` as alternatives.
  • Bounded-Check Function Interfaces (Annex K): Defined a series of safe functions with the `_s` suffix, such as `fopen_s()`, `strcat_s()`, `strcpy_s()`, etc. These functions require the caller to pass the buffer size, preventing buffer overflow issues at the source.
  • More Floating-Point Handling Macros: Extended the floating-point handling macros in `` and ``, providing finer control over floating-point operations and exception handling capabilities to meet the needs of high-precision scientific computing.
  • Anonymous Struct/Union Support: Allows nesting unnamed structs or unions within a struct or union, and accessing their members without an intermediate layer name, 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, it produces a compile-time error (rather than a runtime error). It is commonly used to verify compile-time assumptions like type sizes and struct offsets, improving code robustness.
  • New Exclusive Mode `"x"` for `fopen()`: Appending `"x"` to the file opening mode string (e.g., `"wx"`) indicates exclusive creationβ€”fails if the file already exists, similar to the `O_CREAT|O_EXCL` flag in POSIX. It is often used in scenarios requiring safe temporary file creation.
  • New `quick_exit()` Function: Provides a third way to terminate a program (the other two are `return` and `exit()`). When `exit()` cannot complete cleanup normally, `quick_exit()` will execute the handler functions registered via `at_quick_exit()` and then terminate immediately, suitable for emergency exit scenarios in multithreaded programs.
← Event OnfocusinKeys Randomkey β†’