Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Introduction

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.

About C

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:

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

- `_Thread_local` thread-local storage type specifier; 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. - `_Atomic` type qualifier and ` ` header, providing lock-free atomic operations, an important foundation for multithreaded programming.
  • Enhanced Unicode support: based on ISO/IEC TR 19769:2004, adds char16_t and char32_t data types corresponding to UTF-16 and UTF-32 encodings respectively, and provides a header containing Unicode string conversion functions, improving C's support for internationalized programming.

  • Removes the gets() function: Because gets() 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 safer fgets() or the newly added gets_s() as a replacement.

  • bounds-checking function interfaces (Annex K): defines a series of safe functions with _s suffixes, such as fopen_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 and floating-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 the O_CREAT|O_EXCL flag in POSIX, commonly used for secure 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() executes the handlers registered via at_quick_exit() and then terminates immediately, suitable for emergency exit scenarios in multithreaded programs.