History

The C programming language was originally invented as a tool for developing the Unix operating system.

In 1969, Ken Thompson and Dennis Ritchie at Bell Labs in the United States developed the Unix operating system. Unix was initially written in assembly language, making it difficult to port to other computers. To address this, they decided to rewrite it in a high-level language. However, existing high-level languages at the time did not meet their needs, so Thompson developed the B language based on BCPL.

In 1972, Dennis Ritchie and Brian Kernighan redesigned the B language, creating a new language that replaced B, which they named C.

By 1973, the entire Unix system was rewritten in C, and the language quickly gained popularity, becoming widely used for developing various operating systems and system software.

In 1988, the American National Standards Institute (ANSI) officially standardized the C language, marking its stabilization and formalization.

Decades later, C remains one of the most widely used and popular system programming languages, with Unix and Linux systems still developed in C.


Features of C Language

The enduring popularity and widespread application of C can be attributed to several distinctive features:

  1. Low-Level Language
    C allows direct hardware manipulation, memory management, and system-level operations, making it a language close to the machine level, or “low-level.” This makes it ideal for writing programs that interact closely with hardware or require high performance.
  2. Portability
    C was originally designed to help port Unix to different computer architectures, emphasizing portability from the start. C programs can be relatively easily adapted to run on different hardware and operating systems.
    Beyond traditional computing, C is also the preferred language for embedded systems, such as those in cars, cameras, and home appliances, largely due to its portability.
  3. Simplicity
    C syntax is relatively straightforward, with a limited number of syntax rules and minimal syntactic sugar. Generally, C provides only one way to perform similar tasks, reducing language complexity.
    Furthermore, C syntax is foundational, lacking advanced data structures like classes. Complex data structures must be manually constructed by the programmer.
  4. Flexibility
    C imposes few constraints on programmers. It assumes that programmers know what they are doing, allowing them to perform risky operations without restriction. You have complete freedom, but you are also responsible for the consequences.
    The philosophy of C is “trust the programmer, don’t interfere.” For example, C requires manual memory management and does not offer features like automatic memory cleanup, type checking, array bounds checking, or pointer safety measures.
    While this approach can seem dangerous, it provides experienced programmers with great flexibility, though it can make debugging more challenging.
  5. Summary
    These features allow C to write highly performant programs that fully utilize hardware potential, with a relatively simple compiler implementation. However, C code is prone to errors, and writing reliable programs can be challenging for most programmers.
    Additionally, many modern languages, including C++, Java, C#, and JavaScript, are built on the foundations of C. Mastering C can deepen your understanding of these languages.

Versions of C Language

Throughout history, C has evolved through several versions:

  1. K&R C
    K&R C refers to the original version of the C language. In 1978, C’s creators, Dennis Ritchie and Brian Kernighan, co-authored the seminal textbook The C Programming Language. Since there was no formal C syntax standard at the time, this book became the de facto standard, named after the initials of its authors: “K&R C.”
  2. ANSI C (also known as C89 or C90)
    The original C language was simple and vague in many aspects, and with C syntax still evolving, there were increasing calls for standardization.
    In 1989, ANSI developed a formal C standard, which was later adopted by the International Organization for Standardization (ISO) in 1990. This version is known as “ANSI C” or, by its release years, “C89” or “C90.”
  3. C95
    In 1995, ANSI updated the 1989 standard with support for multibyte and wide characters, resulting in the C95 version.
  4. C99
    The first major revision of the C standard came in 1999, introducing many new features, such as the // comment syntax. C99 is currently one of the most widely used C versions.
  5. C11
    In 2011, the standard was revised again, adding support for Unicode and multithreading. This version is called C11.
  6. C17
    The C11 standard was patched in 2017, with the new version, released in 2018, fixing some of C11’s shortcomings without adding new features. This version is known as C17.
  7. C2x
    A new version of C is currently under discussion and is expected to be finalized by 2023, at which point it will be called C23.

Compiling C Programs

C is a compiled language, meaning its source code files, which typically end with the .c extension, are not directly executable. They must be translated into binary executable files through a compiler. This process, known as “compilation,” occurs during the compile time and differs from the runtime when the program is actually executed.

The most commonly used C compiler today is the GCC compiler from the Free Software Foundation, which is available for free. This book also uses GCC. Linux and Mac systems can directly install GCC, while Windows users can use MinGW. Alternatively, online compilers like CodingGround and OnlineGDB offer web-based simulation of C code execution:

The examples in this book are compiled using GCC from the command line.


Hello World Example

A simple C program, typically named hello.c, is just a plain text file that can be edited with any text editor. Here’s an example:

1
2
3
4
5
6
#include <stdio.h>

int main(void) {
printf("Hello World\n");
return 0;
}

This program’s sole purpose is to display “Hello World” on the screen.

To compile and run C code, ensure GCC is installed, then open the command line and run the following command:

1
$ gcc hello.c

This command compiles hello.c into a binary executable. Note that $ is the command line prompt; you only need to type the part after it.

By default, this command generates an output file named a.out (or a.exe on Windows). Run this file to see the output:

1
2
$ ./a.out
Hello World

The -o option (short for “output”) allows you to specify the output file name:

1
$ gcc -o hello hello.c

This command specifies that the output file should be named hello, instead of the default a.out. Running the file will produce the same result:

1
2
$ ./hello
Hello World

The -std= option allows you to specify the C standard version for compilation:

1
$ gcc -std=c99 hello.c

This command compiles the code according to the C99 standard.

Note: The -std option uses = to connect the argument, unlike -o, which uses a space. Be sure there are no extra spaces around the = sign.