Follow TV Tropes

Following

Media Notes / The C Language

Go To

https://static.tvtropes.org/pmwiki/pub/images/cthrow.png
printf("hello, world!\n");
"C is quirky, flawed, and an enormous success."
Dennis Ritchie

C is an Imperative, Statically-Typed, Procedural Programming Language. It's often considered the mother of modern programming languages, to the point where most non-specialized new languages are either refinements on its strengths or attempts to avoid breaking keyboards over its shortcomings. Or, more often, both. This language, like the Unix system also created at Bell Labs, was developed at a time when computing was done on mainframes by computer black-belts, and it does not hold your hand.

You can try C here.

History

C was co-created by Dennis Ritchie and Ken Thompson to use to help make a port of UNIX. Thompson originally intended to write UNIX in BCPL; however, since he didn't have the official description or documentation, he accidentally developed a much simpler and less wordy version now known as B. Ritchie then further improved B, which resulted in creating a new language, C.note 

C gradually became popular and was implemented on a wide variety of machines during the late 70s and 80s. This popularity fueled the need for standardization, which happened for the first time in 1989 and most recently in 2018. C17 is, and will be the current standard until 2023 when the community votes again.

Usage

C, sometimes alongside its close sibling C++, remains the bedrock of the Unix (and by extension its descendants, including macOS and Linux and all their descendants) and Microsoft Windows operating systems to this day. C and Unix in particular are so intertwined that C compilers on non-Unix systems often include tools to duplicate as much of a Unix environment as possible, including make and even Unix-like shells.

C's speed and precision also made it a popular choice for video game programming. Though C++ has mostly supplanted 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 allowed 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, Marathon 2, Descent, and Dark Forces were all written in C, as were the first three Quake games, along with non-shooty games such as SimCity. 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.

With the advent of higher level and safer languages, however, 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, bare-bones 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 and direct hardware access of low-level code have graduated to one of its higher levels: its direct descendants like C# or Java; languages like Rust or Go; scripting languages like Perl, Ruby, or Python; or premade frameworks like Qt (for applications) or Unity (for games). These are considered quicker to write code for and offer safer alternatives to raw C coding.

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 operating system 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 accessnote  unnecessary, which fueled the need for languages with greater abstraction.

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


    Code Examples 

Hello C

#include <stdio.h>
int main() {
    printf("Hello World!");
    return 0;
}

This program outputs Hello World to the console. You must use double quotes.

Variable Declaration and Concatenation

#include <stdio.h>
int main() {
    int a = 10;
    int b = 7;
    char* c = "hi";
    printf("%d\n", a + b);
    //Outputs 17
    printf("%d\n", b + c);
    //Outputs 4195995
    printf("%s\n", b + c);
    //Outputs the first string it finds depending on the code below
    printf("Hello and %s", c);
    //Outputs Hello and hi
    return 0;
}

This is where C trusting the programmer really comes into play (and possibly back to bite them). C will not throw an error if you try to add a number and a stringnote  together; instead, it will cast it based on the format specifier provided (%d, %s, etc). Meaning the string "c" will be converted to its numerical equivalent and then added to b.

For Loops

#include <stdio.h>
int main() {
    for (int i = 0; i < 10; i++) {
printf("%d\n", i);
    }
    return 0;
}

Outputs numbers 0 thru 9. This three-statement syntax for a for loop originated from C's predecessor B, but became widely known and imitated as a "C-like for loop" due to C's popularity.

Works Written in C

    Video Games 

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.


Top