Skip to main content

Introduction

History

The C language was originally invented as a development tool for Unix systems.

In 1969, Ken Thompson of Bell Labs in the USA developed the Unix operating system with Dennis Ritchie. Unix was written in assembly language, which was not portable to other computers, and they decided to rewrite it in a high-level language. However, the high level languages available at the time did not meet their requirements and Thompson invented the B language based on the BCPL language.

In 1972, Dennis Ritchie and Brian Kernighan redesigned a new language based on B. This new language replaced the B language and is therefore called the C language.

In 1973, the entire Unix system was rewritten in C. Since then, the language has begun to circulate rapidly and is widely used in the development of various operating systems and system software.

In 1988, the American National Standards Institute (ANSI) officially standardised the C language, marking the beginning of its stability and standardisation.

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

Features of the C language

The main reason for the longevity and widespread use of C is that it has a number of distinctive features.

(1) Low-level language

The ability to manipulate hardware, manage memory and talk to the operating system directly makes C a very low-level language, which makes it ideal for writing programs that require interaction with hardware and have extremely high performance requirements.

(2) Portability

The C language was originally designed for porting Unix systems to other computer architectures. This has made it very portable from the start, and C programs can be ported to a variety of hardware architectures and operating systems with relative ease.

In addition to computers, C is now the programming language of choice for embedded systems. The underlying systems of devices such as cars, cameras and household appliances are all programmed in C, again because of its good portability.

(3) Simplicity

The syntax of C is relatively simple, with not too many syntax rules and almost no syntactic sugar. Generally speaking, if two syntaxes can do almost the same thing, C will only provide one, which greatly reduces the complexity of the language.

Furthermore, the syntax of C is basic and does not provide advanced data structures, for example, there are no classes in C and complex data structures need to be constructed by the language itself.

(4) Flexibility

The C language places few restrictions on the programmer. It assumes that the programmer knows what he or she is doing, and does not restrict you from doing anything dangerous; you can do whatever you want, and you are responsible for the consequences.

The philosophy of C is "trust the programmer, don't get in their way". For example, it lets programmers manage their own memory and does not provide automatic memory cleanup. It also does not provide protections such as type checking, negative index checking for arrays, or pointer location checking.

On the face of it, this may seem dangerous, but for advanced programmers there is greater programming freedom. However, it also makes debugging in C less easy.

(5) Summary

The above features allow C to write very high performance programs that exploit the full potential of the hardware, and the C compiler is relatively easy to implement. On the other hand, however, C code is error-prone and not easy for the average programmer to write well.

In addition, many popular contemporary languages are based on C, such as C++, Java, C#, JavaScript and so on. Learning C will help you gain a better understanding of these languages.

Versions of the C language

There have been several versions of the C language throughout history.

(1) K&R C

K&R C' refers to the original version of C. In 1978, Dennis Ritchie and Brian Kernighan, the inventors of the language, wrote a famous textbook called The C programming language. As there is no written syntax standard for C, this book has become the accepted standard and is referred to as "K&R C" after the initials of the two authors.

(2) ANSI C (also known as C89 or C90)

The original version of the C language was very simple and vague in its description of many situations, and with the C syntax still evolving rapidly, there was a growing call for standardisation of the C language.

In 1989, the American National Standards Institute (ANSI) developed a standard for the C language, which was adopted by the International Organisation for Standardisation (ISO) in 1990. It is known as "ANSI C" or, depending on the year of publication, as "C89 or C90".

(3) C95

In 1995, the American National Standards Institute supplemented the 1989 standard by adding support for multi-byte characters and wide characters. This version is called C95.

(4) C99

The first major revision of the C language standard, which took place in 1999, added many language features such as the double-slash (//) comment syntax. This version is called C99 and is the most popular version of C today.

(5) C11

In 2011, the ISO revised the C language once again, adding Unicode and multi-threading support. This version is called C11.

(6) C17

The C11 standard was patched in 2017, but the release was in 2018. The new version only addresses some of the flaws in C11 and does not introduce any new features. This version is called C17.

(7) C2x

The next version of the C language is being discussed by standardisation organisations and is said to be likely to be adopted in 2023, when it will be called C23.

Compilation of the C language

C is a compiled language and the source code is all text files, which cannot be executed by themselves. It must be passed through a compiler, which generates a binary executable file that can be executed. The process by which the compiler translates the code from text to binary instructions is called the compilation phase, also known as "compile time", and is distinguished from the runtime phase (also known as "runtime").

Currently, the most common compiler for C is the GCC compiler from the Free Software Foundation, which is free to use. GCC can be installed directly on Linux and Mac systems, and MinGW can be installed on Windows systems, but there are online compilers that can simulate running C code directly on a web page to see the results, and here are two such tools.

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

Hello World example

Source code files in C usually end with the suffix .c. Here is a simple C program hello.c. It is just a normal text file that any text compiler can use to write.

#include <stdio.h>

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

The only thing this program does is to display "Hello World" on the screen.

This is not an explanation of what the code means, it is just an example of how C code should be compiled and run. Assuming that you have the GCC compiler installed, you can open the command line and execute the following command.

gcc hello.cc

The above command uses the gcc compiler to compile the source file hello.c into binary code. Note that `$ is the command line prompt and all you really need to type is the part after ```.

After running this command, a compilation product file a.out (short for assembler output, or a.exe on Windows platforms) will be generated in the current directory by default. Executing this file will output Hello World on the screen.

$ . /a.out
Hello World

GCC's -o parameter (short for output) specifies the name of the compiled product file.

gcc -o hello hello.c

The -o hello command above specifies that the compiled product will be named hello (replacing the default a.out). The compilation will then produce an executable file called hello, which is equivalent to the name specified for a.out. Executing this file will give you the same result.

$ . /hello
Hello World

GCC's -std= parameter (short for standard) also specifies which C language standard to compile against.

gcc -std=c99 hello.c

The above command specifies that it compiles to the C99 standard.

Note that -std needs to be followed by the = argument, not a space as in -o above, and there should be no extra spaces before or after =.