How do you tell a computer exactly what to do, in a form a person can read, and still have the program run fast? That question is why the C language exists.
The problem: talking to a machine
A processor is the chip that carries out instructions. It understands only machine code, long patterns of binary numbers, and every family of processors has its own machine code. Writing it by hand is slow and full of mistakes.
Assembly language replaces the numbers with short names such as ADD and MOVE. It is easier to read, but it is still tied to one kind of processor, so a program written for one machine must be rewritten for another.
Higher-level languages let you write something close to a formula, such as total = price + tax, and a program called a compiler translates it into machine code. Many of them, however, were too slow or too far from the hardware to build an operating system with. Unix, for example, began its life in assembly language.
Dennis Ritchie and Bell Labs
Bell Labs was the research laboratory of the American telephone company AT&T. In 1969 two researchers there, Ken Thompson and Dennis Ritchie, began work on an operating system called Unix. Thompson wrote a small language called B for it. B had no data types: every value was just a machine word. That became a problem on the newer PDP-11 computer, which handled single bytes as well as words.
Between about 1971 and 1973 Ritchie extended B with types and other features and called the result C, the letter that follows B. By 1973 most of the Unix kernel (the core of the operating system) had been rewritten in C. That was a turning point: Unix could now be moved to a new machine by writing a C compiler for it and changing only a small part of the code. An operating system had become portable.
In 1978 Brian Kernighan and Dennis Ritchie published the book The C Programming Language, which spread the language worldwide. The American standards body ANSI published the first official standard for C in 1989, and newer versions such as C99 and C11 followed. Ritchie and Thompson received the Turing Award, one of computing’s highest honours, in 1983. Ritchie died in 2011.
What makes C special
- Small. C has only a few dozen keywords. Input and output come from libraries, which are ready-made collections of code.
- Fast. C compiles straight to machine code and adds little extra work at run time.
- Close to the hardware. You can work with memory addresses and individual bits.
- Portable. The same C code can be compiled for many different processors.
- A base for other languages. C++ grew out of C, and the curly-brace style of Java, C# and JavaScript is borrowed from it. The most widely used version of Python is itself written in C.
The price is that C trusts you completely. It will not stop you from reading past the end of an array or from forgetting to give memory back. Learning C teaches you what really happens inside a computer, which helps in every other language.
Today C runs operating system kernels such as Linux, embedded systems (the small computers inside washing machines, cars and electricity meters), compilers, device drivers and databases such as SQLite and PostgreSQL.
What a compiler does to one line
Take the line total = price + tax;. A compiler turns it into a few machine steps like these (the real instruction names differ for every processor).
- Load
pricefrom memory into a register, a tiny storage slot inside the processor. - Add
taxto it. - Store the answer in the memory place called
total.
You write one line and the processor performs three steps. Compile the same line for another kind of processor and you get that processor’s own steps. That is the portability of C in a single picture.
C in Indian classrooms and careers
Programming in C is a first-year paper in many BCA, B.Sc. Computer Science, diploma and B.Tech courses in India. “What will this program print?” questions on C are common in campus placement tests, and C is part of the GATE Computer Science syllabus. Beyond exams, C is the language of embedded devices and Linux.
Key points
- Dennis Ritchie created C at Bell Labs in the early 1970s, mainly to write Unix.
- Rewriting Unix in C made an operating system portable.
- A compiler translates C source code into machine code.
- C is small, fast and close to the hardware, which gives power but little protection.
- C is still used for operating systems, embedded devices, compilers and databases.