Follow TV Tropes

Following

Context MediaNotes / TheCLanguage

Go To

1[[quoteright:250:https://static.tvtropes.org/pmwiki/pub/images/cthrow.png]]
2[[caption-width-right:250:printf("hello, world!n");]]
3->''"C is quirky, flawed, and an enormous success."''
4-->--'''Dennis Ritchie'''
5
6C is an [[https://en.wikipedia.org/wiki/Imperative_programming Imperative]], [[https://en.wikipedia.org/wiki/Type_system#STATIC Statically-Typed]], [[https://en.wikipedia.org/wiki/Procedural_programming Procedural]] MediaNotes/ProgrammingLanguage. 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 Platform/{{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.
7
8You can try C [[https://www.onlinegdb.com/online_c_compiler here]].
9
10!!History
11
12C was co-created by Dennis Ritchie and Ken Thompson to use to help make a port of Platform/{{UNIX}}. Thompson originally intended to write UNIX in [[https://en.wikipedia.org/wiki/BCPL 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 [[https://en.wikipedia.org/wiki/B_(programming_language) B]]. Ritchie then further improved B, which resulted in creating a new language, C.[[note]]This became the start of 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 then all bets were off, with languages like C[=--=], D, F#, J#, and R.[[/note]]
13
14C 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.
15
16!!Usage
17
18C, sometimes alongside its close sibling C++, remains the bedrock of the Platform/{{Unix}} (and by extension its descendants, including Platform/MacOS and Linux and all ''their'' descendants) and Platform/MicrosoftWindows {{MediaNotes/operating system}}s 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.
19
20%%As Unix's popularity grew in academia, C displaced Fortran as the language of choice for scientific computing, before being itself displaced by MediaNotes/{{Python}}, MATLAB, R, Mathematica and C++.
21
22C'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.
23
24The resulting explosion of light and sound was the FirstPersonShooter boom: ''{{VideoGame/Doom}}'', ''[[{{VideoGame/Marathon}} Marathon 2]]'', ''{{VideoGame/Descent}}'', and ''VideoGame/DarkForces'' were all written in C, as were the first three ''{{VideoGame/Quake}}'' games, along with non-shooty games such as ''VideoGame/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 Creator/CrypticStudios has been known to use almost-entirely pure C in their games, such as ''VideoGame/CityOfHeroes'' and ''VideoGame/StarTrekOnline''.
25
26With 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 MediaNotes/{{Rust}} or Go; scripting languages like Perl, MediaNotes/{{Ruby}}, or MediaNotes/{{Python}}; or premade frameworks like [[https://en.wikipedia.org/wiki/Qt_(software) Qt]] (for applications) or MediaNotes/{{Unity}} (for games). These are considered quicker to write code for and offer safer alternatives to raw C coding.
27
28%%!!Just Following Procedure
29
30%%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.
31
32%%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 {{Platform/UNIX}} at [=AT&T's=] Bell Labs, and to this day it remains a popular language for operating systems. Both Platform/MicrosoftWindows and the Platform/{{Linux}} kernel are written in C, and [[Platform/MacOS OSX]] is written in a variant called "Objective-C".
33
34%%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]]C++ tends to be more pedantic with its casting, requiring an explicit cast where C would cast implicitly, and reserves more words as keywords which breaks any code which uses them as names. It also maintains less compatibility with older pre-1989 C code. C++ compilers (such as Microsoft Visual C++) tend to include C modes/compilers as standard, though.[[/note]] However, it's something of a BerserkButton 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 (at least supposed to be) part of an object, which is concealed from the rest of the code. In practice, however, due to many codebases starting out as C before being upgraded to C++ at some point in their history, it's common to see projects that mix both languages' conventions and coding styles.
35
36!!No Assembly Required
37
38A 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.
39
40Once 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.
41
42C 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 [[DarthWiki/IdiotProgramming shoot themselves in the foot]], and is often a source of {{Game Breaking Bug}}s.
43
44In 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[[note]]aside from specialized tasks like interfacing with custom peripherals[[/note]] unnecessary, which fueled the need for languages with greater abstraction.
45
46Higher level programming languages like MediaNotes/{{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.
47
48[[hardline]]
49
50[[folder:Code Examples]]
51
52!!!Hello C
53@@[[purple:#include]] [[gray:<stdio.h>]]\
54[[purple:int]] [[gray:main() {]]\
55    [[teal:printf]][[gray:(]][[blue:"Hello World!"]][[gray:);]]\
56    [[purple:return]] [[blue:0]][[gray:;]]\
57[[gray:}]]\
58@@
59\
60This program outputs Hello World to the console. You must use double quotes.
61
62!!!Variable Declaration and Concatenation
63
64@@[[purple:#include]] [[gray:<stdio.h>]]\
65[[purple:int]] [[gray:main() {]]\
66    [[purple:int]] [[gray:a]] = [[blue:10]][[gray:;]]\
67    [[purple:int]] [[gray:b]] = [[blue:7]][[gray:;]]\
68    [[purple:char]]* [[gray:c]] = [[blue:"hi"]][[gray:;]]\
69    [[teal:printf]][[gray:(]][[blue:"]][[teal:%dn]][[blue:"]][[gray:, a + b);]]\
70    [[green://Outputs 17]]\
71    [[teal:printf]][[gray:(]][[blue:"]][[teal:%dn]][[blue:"]][[gray:, b + c);]]\
72    [[green://Outputs 4195995]]\
73    [[teal:printf]][[gray:(]][[blue:"]][[teal:%sn]][[blue:"]][[gray:, b + c);]]\
74    [[green://Outputs the first string it finds depending on the code below]]\
75    [[teal:printf]][[gray:(]][[blue:"Hello and ]][[teal:%s]][[blue:"]][[gray:, c);]]\
76    [[green://Outputs Hello and hi]]\
77    [[purple:return]] [[blue:0]][[gray:;]]\
78[[gray:}]]\
79@@
80\
81This 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 string[[note]](well, technically a char array)[[/note]] 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.
82
83!!!For Loops
84
85@@[[purple:#include]] [[gray:<stdio.h>]]\
86[[purple:int]] [[gray:main() {]]\
87    [[purple:for]] [[gray:(]][[purple:int]] [[gray:i]] = [[blue:0]][[gray:; i]] < [[blue:10]][[gray:; i]]++[[gray:) {]]\
88 [[teal:printf]][[gray:(]][[blue:"]][[teal:%dn]][[blue:"]][[gray:, i);]]\
89    [[gray:}]]\
90    [[purple:return]] [[blue:0]][[gray:;]]\
91[[gray:}]]\
92@@
93\
94Outputs 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.
95[[/folder]]
96
97!!Works Written in C
98
99[[folder:Video Games]]
100* [[https://floppy.foone.org/w/BRender The [=BRender=] engine]], which powered ''VideoGame/{{Carmageddon}}'', ''VideoGame/{{Croc}}'', and ''Doctor Who: Destiny of the Doctors''
101* ''VideoGame/CityOfHeroes''
102* ''VideoGame/DarkForces''
103* ''{{VideoGame/Descent}}''
104* ''{{VideoGame/Doom}}''
105* ''VideoGame/DukeNukem3D''[[note]]The source code is out there, but be warned: it is an EldritchAbomination that [[GoMadFromTheRevelation makes all who gaze upon it want to tear their eyes out in horror]].[[/note]]
106* ''[[{{VideoGame/Marathon}} Marathon 2]]''
107* ''Franchise/{{Pokemon}} [[Platform/GameBoyAdvance Generation III,]] [[Platform/NintendoDS IV and V]] main series games. ''[[labelnote:List of Games]][[VideoGame/PokemonRubyAndSapphire Ruby, Sapphire & Emerald]], [[VideoGame/PokemonRedAndBlue Fire Red & Leaf Green]], [[VideoGame/PokemonDiamondAndPearl Diamond, Pearl & Platinum]], [[VideoGame/PokemonGoldAndSilver HeartGold & SoulSilver]], [[VideoGame/PokemonBlackAndWhite Black & White]], [[VideoGame/PokemonBlack2AndWhite2 Black 2 & White 2]].[[/labelnote]]
108* ''VideoGame/SimCity''
109* ''VideoGame/StarTrekOnline''
110* ''Videogame/SuperMario64''[[note]]Not released as open-source by Nintendo, but fans managed to reverse-engineer working C source code from the ROM in 2019[[/note]]
111* ''VideoGame/TheLegendOfZeldaOcarinaOfTime''[[note]]Like ''Super Mario 64'' before it, fans have managed to decompile the game's C source code.[[/note]]
112* ''{{VideoGame/Quake}}'' 1, 2 and 3
113
114[[/folder]]
115
116If 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. [[https://github.com/id-Software/wolf3d Wolfenstein 3D]], [[https://github.com/id-Software/DOOM Doom]], and [[https://github.com/id-Software/Quake Quake]] and [[https://github.com/id-Software/Quake-2 its]] [[https://github.com/id-Software/Quake-III-Arena 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.
117

Top