Why choose C language in history? Facts have proved: the C language in the old age is still not old!

The emergence of any new thing is not from accident, but the inevitable result driven by the times.

1. The development of C language

1. How great is the C language

If you ask me: how great is the C language. Then, I might think about it and say: I don’t know how great, but I know how great.

Here, I want to say something that may be a bit one-sided, that is: Nowadays, where there is electricity in the world, there may be her (C language) or her descendants.

Any language lower than C language is not enough to completely abstract a computer system; any language higher than C can be implemented in C.

2. The father of C language

Dennis MacAlistair Ritchie (English: Dennis MacAlistair Ritchie, September 9, 1941-October 12, 2011) was an American computer scientist. Hackers usually call him "dmr". He is the creator of the C language and a key developer of the Unix operating system. He has had a profound impact on the computer field and was the winner of the 1983 Turing Award with Ken Thompson.

Dennis Ritchie's life timeline

Professor Martin from the Department of Computer Science at MIT commented: "If Jobs is the king of visualization products, then Rich is the monarch of the invisible kingdom. Jobs’ contribution is that he understands the needs and desires of users so much. As for the creation of technological products that make contemporary people reluctant to think about it. However, it is Mr. Rich that provides the most core components for these products. People cannot see these components, but they use them every day." Knihan commented: Newton said he was standing on the shoulders of giants. Now, we are all standing on Rich's shoulders.

3. The ancestors of C language

For the sake of brevity, I (Dennis M. Ritchie) omitted the complete description of C itself, its parent B [Johnson 73] and its grandparent BCPL [Richards 79], and only focused on the characteristic elements of each language and how they evolved . This paper is about the development of the C programming language, the influences on it, and the conditions under which it was created. For the sake of brevity, I omit full descriptions of C itself, its parent B [Johnson 73] and its grandparent BCPL [Richards 79], and instead concentrate on characteristic elements of each language and how they evolved.

This text comes from an article about "The History of C Language Development" written by Dennis M. Rich, the father of the C language. The text clearly points out that the C language originated from two languages, B and BCPL. C language can be regarded as standing on the shoulders of giants, following the tide of the times.

1) Father of BCPL language

Martin Richards

Martin Richards (English: Martin Richards, July 21, 1940 -), born in the United Kingdom, a computer scientist, as the inventor of the BCPL programming language, developed the TRIPOS operating system.

In 1966, Martin Richard invented the BCPL programming language based on the CPL programming language at Cambridge University.

2) The father of B language

Kenneth Lane Thompson

Kenneth Lane Thompson (English: Kenneth Lane Thompson, February 4, 1943 -) nicknamed Ken Thompson (English: Ken Thompson), American computer science scholar and engineer. Hacker culture circles usually call him "ken". While working at Bell Labs, Thompson designed and implemented the Unix operating system. He created the B language (based on BCPL)-the predecessor of the C language, and he is one of the creators and developers of the Plan 9 operating system. Together with Dennis Ritchie, he was the winner of the 1983 Turing Award.

In 2006, Thompson joined Google and co-designed the Go language.

3) A set of photos of Ken and Dennis

Improving and standing on the shoulders of giants

Ken is sitting and Dennis is standing

Teach by example

Thanks to Dennis Rich for leaving a "C language Bible" to the world

It’s a pity that the author studied the C language of Tan Haoqiang at the university. 

Dennis与《The C Programming Language》

4. C language timeline

From the timeline in the figure, we can clearly see the origin and time nodes of the C language.

Sometimes I have to say that times make heroes. Between 1969 and 1971, the famous operating system Unix was born from Ken Thompson. As a large-scale system software, a reliable high-level language is extremely needed. Appeared (the low-level language at the time referred to assembly, because the previous operating system was written in assembly). Dennis Rich was not idle at this time. After the improvement of the B language, a typed C language was born (according to Rich himself, for a while, this improved language was called NB. Namely: new B. However, in the eyes of those of us who eat melons, it is indeed NB).

In 1971 I began to extend the B language by adding a character type and also rewrote its compiler to generate PDP-11 machine instructions instead of threaded code. Thus the transition from B to C was contemporaneous with the creation of a compiler capable of producing programs fast and small enough to compete with assembly language. I called the slightly-extended language NB, for `new B.'

5. Unix timeline

The birth of Unix is ​​closely related to the widespread dissemination and use of the C language.

The above timeline only shows the first few Unix versions that were born in the same time period as the C language (Of course, if you are interested, you can query the history of Unix related development, which will definitely surprise you. The most famous branches: BSD , Minix, Linux...).

Below, through some of the Unix kernel source code found on the Internet, I will trace the time when the C language appeared.

1) PDP-Unix

PDP-Unix system kernel code file

PDP-7 Unix 

 

It can be seen that they are basically written in assembly (file name suffix .s). Why use the word basic? Because some commands in the system are written in B language.

2)First Edition Unix

First Edition Unix system kernel code file

As you can see, it is still written in assembly (file name suffix.s).

3) Second Edition Unix

Second Edition Unix system kernel code file

Second Edition Unix The second edition of Unix was developed for the PDP-11 at Bell Labs by Ken Thompson, Dennis Ritchie and others. It extended the First Edition with more system calls and more commands. This edition also saw the beginning of the C language, which was used to write some of the commands.

By this version, you can already see the C language.

4) Unix and C language

After the C language appeared in the Unix V2 version and the Unix V3 version, a large number of codes written in the C language can already be seen in the Unix kernel.

According to Richie: By early 1973, the basic knowledge of modern C had been completed. The C language and compiler are powerful enough to allow us to rewrite the Unix kernel of PDP-11 (that is, the V3 version of Unix) in C that summer.

By early 1973, the essentials of modern C were complete. The language and compiler were strong enough to permit us to rewrite the Unix kernel for the PDP-11 in C during the summer of that year.

At this point in time, it is basically certain that most of the cores of C language and Unix have been perfected. All that remains to be done is portability and standardization.

You may have heard of the following story: Later, academic and government organizations were using Unix, and it was precisely because of the popularity and prosperity of Unix that the C language was widely spread and used.

In the 1980s, the use of the C language spread widely, and almost all machine architectures and operating systems can use compilers. In particular, it has become a programming tool for personal computers, and it is very popular whether it is commercial software manufacturers for these machines or end users interested in programming.

During the 1980s the use of the C language spread widely, and compilers became available on nearly every machine architecture and operating system; in particular it became popular as a programming tool for personal computers, both for manufacturers of commercial software for these machines, and for end-users interested in programming.

This is the so-called mutual achievement.

The summary is: not afraid of gains and losses, getting better and better, and finally achieving each other.

6. How was the first C language compiler written?

I don’t know if you have ever thought about it. Everyone writes compilers in C language or a language based on C. So how did the world’s first C language compiler write it? This is not a "chicken and egg" question...

Looking back at the history of C language: Tomphson developed the B language on the basis of BCPL, and Ritchie successfully developed the current C language on the basis of the B language. Before C language was used as a system programming language, Tomphson also used B language to write operating systems. It can be seen that before C language is implemented, B language can already be put into practical use. Therefore, the prototype of the first C language compiler may be written in B language or mixed B language and PDP assembly language.

We all know now that the execution efficiency of the B language is relatively low, but if it is all written in assembly language, not only the development cycle is long, the maintenance is difficult, but also the portability necessary for high-level programming languages ​​is lost.

Therefore, the early C language compilers took a tricky approach: first use assembly language to write a subset of the C language compiler, and then recursively through this subset, and then complete the complete C language compiler.

The detailed process is as follows: first create a subset with only the most basic functions of the C language, denoted as the C0 language, the C0 language is simple enough, you can directly write a C0 compiler in assembly language. Relying on the existing functions of C0, the design is more complex than C0, but still incomplete, another subset of the C language C1 language, where C0 belongs to C1, C1 belongs to C, and C0 is used to develop a C1 language compiler. On the basis of C1, design another subset of C language C2 language. C2 language is more complex than C1, but it is still not a complete C language. A compiler for C2 language was developed... So until CN, CN is already powerful enough, this Time is enough to develop a complete C language compiler implementation. As for the N here, it depends on the complexity of your target language (here is C language) and the programmer's programming ability. Simply put, if you reach a certain subset stage, you can easily use the existing functions to implement the C language, then you have found N. The following diagram illustrates this abstract process:

https://kknews.cc/tech/bx2r3j.html introduces a concept, "self-compilation" Self-Compile, that is, for some strong types with obvious bootstrap properties (the so-called strong type means that every variable in the program must It can be used only after declaring the type, such as C language. On the contrary, some scripting languages ​​do not have the term type at all.) Programming languages ​​can use a limited subset of them to achieve their own expression through a limited number of recursions. Languages ​​include C, Pascal, Ada, etc. As for why it can be self-compiled, you can refer to "Compilation Principles" by Tsinghua University Press. The book implements a compiler of a subset of Pascal. https://zhuanlan.zhihu.com/p/136102461

And this process also found evidence in the Unix V2 version.

Ken Thompson, Dennis Ritchie, and others developed the second version of Unix for the PDP-11 at Bell Labs. It extends the first edition with more system calls and more commands. This version also saw the beginning of the C language, which was used to write some commands.

The code here is just some commands, some library functions and the source code of the C compiler. The files in c/ come from the last1120c.tar.gz tape and constitute a valid C compiler for the second version of Unix.

The second edition of Unix was developed for the PDP-11 at Bell Labs by Ken Thompson, Dennis Ritchie and others. It extended the First Edition with more system calls and more commands. This edition also saw the beginning of the C language, which was used to write some of the commands.The code here is only the source to some of the commands, some of the library functions, and the C compiler. The files in c/ come from the last1120c.tar.gz tape, and form a working C compiler for Second Edition Unix.

After downloading the source code and decompressing it, the directory structure is as follows:

Interested friends can download it and study it.

Two, BCPL, B, C language comparison

If you want to find a good way to compare programming languages, it's the code.

1. Three language code examples

The following uses BCPL, B, C three languages ​​to implement a simple program: the program adds three numbers a, b, and c, assigns the result to sum, and finally prints the sum.

1) BCPL language example

GET "libhdr"

LET start() = VALOF

{ LET a, b, c = 1, 2, ,3

sum := a + b + c

writen(sum)

}

LET declare variables;

The := symbol is an assignment symbol. This symbol is also found in Go, which represents a local variable inside a function. The interesting thing here is that Ken Thompson, the father of the B language, changed the := symbol to the = symbol. Now, as one of the fathers of the Go language, I have brought back the := symbol (destiny in the dark~).

从BCPL到B的过渡中,决定使用单个字符 = 代替赋值 :=Other fiddles in the transition from BCPL to B were introduced as a matter of taste, and some remain controversial, for example the decision to use the single character = for assignment instead of :=. Similarly, B uses /**/ to enclose comments, where BCPL uses //, to ignore text up to the end of the line. The legacy of PL/I is evident here. (C++ has resurrected the BCPL comment convention.) Fortran influenced the syntax of declarations: B declarations begin with a specifier like auto or static, followed by a list of names, and C not only followed this style but ornamented it by placing its type keywords at the start of declarations.

2) B language example

Language structure of B language

main() {

-- statements --

}

newfunc(arg1, arg2) {

-- statements --

}

fun3 (arg) {

-- more statements --

}

B language code example

main() {

auto a, b, c, sum;

a = 1; b = 2; c = 3;

sum = a+b+c;

putnumb(sum);

}

The statement auto... is a statement. That is, it defines the local variables to be used in the function;

putnumb is a library function with parameters, which will print a number on the terminal.

3) C language example

#include <stdio.h>

void main(){

int a,b,c,sum;

a=1; b=2; c=3;

sum = a+b+c;

printf("%d", sum);

}

2. The difference between the three in the sample code

The difference between the three can be seen through the above example:

C language writing is closer to B language;

BCPL and B languages ​​are untyped languages, and use word/cell to represent a fixed-length bit. C language has types.

There are some places you might be interested in:

++, - The symbol was invented by Thompson;

&&, || are introduced in C language.

Description:

After checking for a long time, I only found some code fragments of BCPL and B language. As for whether it can run, I don’t know ^_^;

If you want to know the specific differences between the three, it is recommended to read Dennis Ritchie's article on "The History of C Language Development";

BCPL and B languages ​​have also experienced several version iterations (because the code snippets found on the Internet have many different ways of writing. For example, the B code snippets found in Wikipedia are different from those found in previous versions of the Unix kernel. B code snippets are written differently. Personal speculation is a version issue, and different versions have different writing methods);

The C language is closer to the B language, or it is to say that a lot of new features are continuously added on the basis of B (2 questions are thrown: 1. Why is Rich not named B++ like C++ when he named it? 2. Why does C++ use 2 plus signs instead of one plus sign, called C+? Welcome students with big brains to leave comments!);

If there is a master who thinks the above code snippet is wrong or knows how to run, you can communicate privately.

Third, why choose C language in history

In the late 1960s, Bell Labs research on computer systems entered a prosperous period. The Mutlics project co-operated by MIT, General Electric, and Bell Labs ended in failure (around 1969). It was during this period that Ken Tompson began to write alternatives to Mutlics. He hoped to construct a comfortable computing system (that is, Unix) according to his design. Later, when writing the first version of Unix, he felt that a new system programming language was needed on Unix, and he created a B language. B language is C without any type. To be precise, B language is a language produced by Tompson squeezing BCPL into 8K memory and filtered by his personal brain.

Due to some problems in the B language, it is only used to write some command tools. It was during this period that Ritchie redesigned and improved on the basis of the B language, thus giving birth to the C language.

In 1973, the C language was basically complete. From the language and compiler level, it was enough for Tompson and Ritchie to use the C language to rewrite the Unix kernel. Later, Unix slowly became popular in some research institutions, universities, and government agencies, which in turn led to the development of the C language.

In 1978, "The C Programming Language" compiled by K&R was published, which further promoted the popularization of C language.

To summarize in one sentence: at the right time and at the right place, the right people and tools appeared (the relationship between Unix and the C language is a bit like the relationship between GNU and Linux kernel, which is mutual achievement).

Fourth, the standard C library and code

After several iterations of the C language and its standards, it became what everyone sees today. Many C standard libraries are specified in the standard, and different systems have their own different code implementations.

Of course, there is also code that implements the standard C library in the Linux kernel. Let's appreciate her beauty together.

1. Standard C library

ANSI C includes 15 header files in total. In 1995, Normative Addendum 1 (NA1) approved 3 header files (iso646.h, wchar.h and wctype.h) to be added to the C standard library. The C99 standard adds 6 header files (complex.h, fenv.h, inttypes.h, stdbool.h, stdint.h and tgmath.h). Five additional header files (stdalign.h, stdatomic.h, stdnoreturn.h, threads.h and uchar.h) have been added to the C11 standard.

So far, there are 29 header files in the C standard library:

2、 linux/lib/string.c

Linux kernel version: 4.18.13

The following lists three string processing functions strcpy(), strncpy(), strncat(). The code comes from Linus Benedict Torvalds, why do you say that? Look at the code header comments, it's the familiar taste stupid. Those who have read the git source code should also know that there are similar comments in the git source code.

// SPDX-License-Identifier: GPL-2.0

/*

*  linux/lib/string.c

*

*  Copyright (C) 1991, 1992  Linus Torvalds

*/

/*

* stupid library routines.. The optimized versions should generally be found

* as inline code in <asm-xx/string.h>

*

* These are buggy as well..

*

* * Fri Jun 25 1999, Ingo Oeser <[email protected]>

* -  Added strsep() which will replace strtok() soon (because strsep() is

*    reentrant and should be faster). Use only strsep() in new code, please.

*

* * Sat Feb 09 2002, Jason Thomas <[email protected]>,

*                    Matthew Hawkins <[email protected]>

* -  Kissed strtok() goodbye

*/

// .......omit other......

// ...... here is my love code  .....

#ifndef __HAVE_ARCH_STRCPY

/**

* strcpy - Copy a %NUL terminated string

* @dest: Where to copy the string to

* @src: Where to copy the string from

*/

#undef strcpy

char * strcpy (char * dest, const char * src)

{

char * tmp = dest;

while ((*dest++ = *src++) != '\0')

/* nothing */;

return tmp;

}

EXPORT_SYMBOL(strcpy);

#endif

#ifndef __HAVE_ARCH_STRNCPY

/**

* strncpy - Copy a length-limited, C-string

* @dest: Where to copy the string to

* @src: Where to copy the string from

* @count: The maximum number of bytes to copy

*

* The result is not %NUL-terminated if the source exceeds

* @count bytes.

*

* In the case where the length of @src is less than  that  of

* count, the remainder of @dest will be padded with %NUL.

*

*/

char *strncpy(char *dest, const char *src, size_t count)

{

char * tmp = dest;

while (count) {

if ((*tmp = *src) != 0)

src++;

tmp++;

count--;

}

return dest;

}

EXPORT_SYMBOL(strncpy);

#endif

#ifndef __HAVE_ARCH_STRLCPY

#ifndef __HAVE_ARCH_STRNCAT

/**

* strncat - Append a length-limited, C-string to another

* @dest: The string to be appended to

* @src: The string to append to it

* @count: The maximum numbers of bytes to copy

*

* Note that in contrast to strncpy(), strncat() ensures the result is

* terminated.

*/

char *strncat(char *dest, const char *src, size_t count)

{

char * tmp = dest;

if (count) {

while (*dest)

dest ++;

while ((*dest++ = *src++) != 0) {

if (--count == 0) {

* dest = '\ 0';

break;

}

}

}

return tmp;

}

EXPORT_SYMBOL(strncat);

#endif

#ifndef __HAVE_ARCH_STRLCAT

// .......omit other......

// ...... here is my love code  .....

When looking at these codes with his classmates for the first time, he said: This is called code, and everything else is shXt. Now I think about it, when I implemented these codes, I wrote something that I didn't know what it was. Hey, code is more tossed than code~

Reading other people's code is also a kind of progress and growth.

5. Lian Po is old, can he still have food?

"Historical Biography of Lian Po Lin Xiangru" records that after Lian Po was dismissed, he ran to Wei. Zhao Wang wanted to use him again and sent someone to see his physical condition. Lian Po’s enemy Guo Kai bribed the messenger, and the messenger saw Lian Po Po, Lian Po made a bucket of rice, ten catties of meat, and was mounted on horses to show that it was still usable. The messenger came back and reported to King Zhao: "Although General Lianpo is old and good at food, he sat with his ministers, and the three stubborn arrows (passing false characters, that is, shit) are gone." King Zhao thought that Lianpo was old, so he didn't use it.

After decades of trials and hardships, the C language can be described as numerous, and it can be seen everywhere in the world. However, at the same time, in the eyes of some people, it may be that the C language is old (nearly 50 years old) and old. As shown below:

If you really think so, then you are wrong.

TIOBE Index for September 2020

The TIOBE 2020-09 programming language rankings tell you that the C language treasure is not old, but still that dazzling youth.

What I want to say is that as long as the computer is still based on the Von Neumann architecture, the chip is still based on the physical process. Then, there will be a C sky. Because she knows a place closest to the sky (C is one of the high-level languages ​​closest to assembly and machine language).

Let him change the dynasty of the upper-level application, I (C language) does not move. This is C, the C language in my heart.

to sum up

Suddenly, I realized that the summary was reached, but I still felt that I still hadn't finished. This is not the truest one in my mind. However, I still hope that after reading this article, you can understand and be familiar with the beauty and truth of C.

The last thing I want to say is: even a thousand words can't say the importance of the C language, these words are just the tip of the iceberg.

In view of the limited personal ability, if there are any problems or defects, please correct me.

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112392751
Old