Follow TV Tropes

Following

Useful Notes / The C Language

Go To

"C is quirky, flawed, and an enormous success."
Dennis Ritchie

The C programming language is a general-purpose programming language, and one of the most popular languages in the world. It's often considered the mother of modern programming languages, to the point most non-specialized new languages are either refinements on its strengths or attempts to avoid breaking keyboards over its shortcomings. Or, more often, both.

Advertisement:

Not to be confused with The C Programming Language, the original specification manual written by the language's developers, Dennis Ritchie and Brian Kernighan. Although some confusion will inevitably result, because diving into the C language will often led to Guide Dang It!. This language, like the Unix operating system it was designed for, was developed at a time when computing was done on mainframes by computer black-belts, and it does not hold your hand.

It was based off of an earlier language called BCPL, more specifically a simpler, modified version called B, from which the name "C" was derived. This triggered a famous Snowclone in computing, with C's own successor being dubbed C++ as a pun, after a command in C to increase a number by one. C++'s own successor would then be named C#, and after that all bets were off, with languages like D, F#, J#, R, X++, and so on. Aside from C++ and C#, two popular languages in their own right, none of these languages even comes close to the success of C.

Advertisement:

Just Following Procedure

Though C is broad enough to be used for many purposes, it is technically known as a "procedural" language. Any C code not designed to induce a headache is broken up into small chunks of self-contained code. The program calls procedures (known in C as functions, but not to be confused with functional programming, which is something else entirely) which perform a smaller subtask and then return control to the part of the code that called it.

Hence, chunks of code can be reused many, many times according to the control flow, the logic built into a program, as opposed to older "batch" programs which would be fed input and spit the results back out at you. C's modular nature comes from its role as a systems language designed to program UNIX at AT&T's Bell Labs, and to this day it remains a popular language for operating systems. Both Microsoft Windows and the Linux kernel are written in C, and OSX is written in a variant called Objective-C.

Advertisement:

When discussing C, it's often rolled up with C++ to become C/C++. Almost all valid C code will run with no problems inside C++, since C++ was designed to wrap around C like a matryoshka doll.note  However, it's something of a Berserk Button for some programmers, who say C++ has its own entirely different coding practices, since C++ is object-oriented rather than procedural. In C, things such as the player's health, location, and weapon are treated as individual datum by the code, which can be changed by any procedure that is directed to change it. In C++ however, that data is instead (supposed to be) part of a monolithic block of data called an "object", which is concealed from the rest of the code. This keeps it safe from accidental alteration.

(That's not to say C is incapable of similar functionality, but it takes extra work to implement, whereas C++ has it right out of the box.)

No Assembly Required

A popular programmer joke says that C "combines the speed and power of an assembly language with the clarity and readability of an assembly language." Assembly here refers to the stream of actual instruction codes running through your processor. Assembly code relies heavily on the physical memory addresses that allow you to swap and store data between the processor and the system's memory.

Once upon a time, assembly was the only way to program, and it had to be tailored to a specific mainframe computer's instruction set and memory layout. Then, programmers started devising higher-level languages such as FORTRAN, ALGOL, and COBOL, which allowed more expressive and abstract syntax and could be translated to different assembly code for different machines.

C came about at the tail-end of this period, and in many ways it serves as a bridge between these languages and newer ones. It's old enough that "pointers", a data type which allow a program to directly manipulate memory addresses, are a key feature, whereas most newer languages abstract that away and keep memory manipulation completely under the hood. This functionality is essential for operating system programming, as an OS is by definition something that manages your hardware for you, but directly manipulating memory is a good way for someone who doesn't know what they're doing to shoot themselves in the foot, and is often a source of Game Breaking Bugs.

In the DOS days, you could only run one program at a time, so it was no sweat for the computer to hand over the hardware's reins so a program could manipulate it. But the rise of multitasking operating systems like Windows, which have to coordinate dozens of different programs contending for system resources, made such low-level hardware access — aside from specialized tasks like interfacing with custom peripherals — unnecessary, which fueled the need for high-level languages like Java that don't require the programmer to use pointers at all.

Newer languages like Java, incidentally, are written and implemented in C/C++ precisely because they can manipulate memory, whereas most other languages can't. The underlying C/C++ code is automated so the programmer doesn't have to worry about minutiae, with extra safety features and easier commands to perform complex tasks.

Modern Times

With the advent of higher-level languages, C started going the way of Latin. It's still considered essential for operating systems, hardware interfacing, and embedded systems (say, a thermostat or a pacemaker), but when it comes to applications it's something of an elder statesman: it's generally accepted as an important pioneer, but it's often sidelined because of its antiquated methods, barebones nature, and propensity for bug-riddled coding. Many commercial programmers and businesses that don't have to work with preexisting C codebases or require the performance boost of low-level code have graduated to its descendants like C# or Java, or premade frameworks like Qt (for applications) or Unity (for games). These are considered quicker to write code for and offer safer alternatives to C coding, such as garbage collection, where the program automatically handles all memory to ensure the programmer doesn't rope off a certain section of memory for a specific purpose and then forget to free it again (a very common mistake in C).

If that sounds like the death knell for C, though, don't be deceived. When charting the popularity of languages via internet searches, it usually sits in the Top 5, and sometimes at #1. How much of that is due to a chicken/egg situation with regards to all the software already written in C is tough to discern, but it still has its devoted followers. It did become popular for a reason: it's highly flexible and very, very fast, and it's broad enough that it can be used in many different contexts if a programmer feels like doing the legwork to implement the higher-level functionality.

Some of its popularity is due to C++, which as mentioned is one of the few object-oriented languages that can also use pointers, having a reputation among detractors for being bloated and unwieldy, but many a programming holy war has been fought over that claim, so let's just say both languages satisfy their passionate fans.

Doom and Boom(stick)

C's renown for speed and precision made it a popular choice for video game programming. After all, there's nowhere you want those things more than on Mars blasting demons. Though C has mostly been supplanted by C++ in game programming now, many of the great games of yesteryear were written in C, especially during the 90s heyday of computer gaming. C's simple power enabled it to get the action moving at a lightning-pace, which coincided nicely with the increasing availability of sound cards and VGA graphics.

The resulting explosion of light and sound was the first-person shooter boom: Doom, Duke Nukem 3D, Marathon 2, Descent, and Dark Forces were all written in C, as were the first three Quake games. Decidedly non-shooty game SimCity was also made with C. Since companies are under no obligation to release details about their source code, it's tough to say who's still using it, but Cryptic Studios has been known to use almost-entirely pure C in their games, such as City of Heroes and Star Trek Online.

Even for game engines written in C++ or other languages, enormously popular libraries such as SDL (Simple DirectMedia Layer, which simplifies interfacing with operating systems) and Open GL (the industry standard for 3D graphics) are written in C, meaning lots of function calls to underlying C code. The popular open source CGI software Blender is also built with a C core, so Big Buck Bunny, Sintel, and Tears of Steel were all modeled, animated, and rendered by C code.

Noticing a pattern? Wherever graphics go, there the C programming language is. Although it's often paired with scripting languages or other toolkits, C remains a backbone of hardware intensive programs due to its ability to manipulate memory, interact with hardware, and run very quickly while doing both those things. That, coupled with its ubiquity in the lower-level architecture of modern information technology, ensures that C remains as vital as it has been for the past half-century. And it will ensure that C remains a vital tool for programmers to shoot themselves in the foot with for the foreseeable future.

Further Reading

If you're interested in reading some treasures of C code for yourself, id Software have a habit of releasing the source code to their old engines. Wolfenstein 3D, Doom, and Quake and its sequels are all available for your viewing pleasure, so you can relive all the nights you spent blasting monsters from the other side of the compiler.

The source code for Duke Nukem 3D is also out there, but be warned: it is an Eldritch Abomination that makes all who gaze upon it want to tear their eyes out in horror.

As for learning the actual C language itself, the best book on the subject is The C Programming Language by Brian Kernighan and Dennis Ritchie, known to programmers as just "K&R".

Top

Example of:

/
/

Feedback