4.9 KiB
C
C is a procedural, compiled programming language created by Dennis Ritchie in 1973 for use in the Unix operating system. Despite being an old language, it remains very relevant and it will stay that way for the next 100 years and more.
When compared with "modern" programming languages, C is a simple and minimal language, since it lacks "modern" features such as generics, memory safety, standard data structures and other high-level constructs, for that it is generally considered a low-level language, even though strictly is a high-level language, because it abstracts over the platform dependent assembly language, effectively turning C into a portable assembly. Detractors are quick to point nonsense such as "C can't be a portable assembly because it forces an abstract model", ignoring the fact that C being so influential that any CPU architecture meant to be taken seriously has a compiler for it (therefore a CPU made to efficiently execute C, incidentally or not).
Additionally, it is quite unforgiving and does not handhold the programmer at all, in part this is good since it forces to actually think what one is doing, unlike in modern languages.
C was created in 1973 by Dennis Ritchie, then working in the Unix operating system, which up to that point was exclusively written in PDP-11 assembly (thus non portable) as a programming language suitable for systems programming.
The name "C" comes from the fact that C is a successor of the earlier B programming language (made by Ritchie and Ken Thompson) which was in turn based in another programming language called BCPL.
Copies of Dennis Ritchie's original C compiler can be found here: https://github.com/mortdeus/legacy-cc and here: http://cm.bell-labs.co/who/dmr/primevalC.html (also includes a brief history of the beginnings of C).
In 1978 the first edition of The C Programming Language was released, describing pre-ANSI C nowadays known as "K&R C".
This version had slight syntax differences, one of them being a different function parameter syntax and extensive use of
the implicit int
type (when no type was specified, it defaulted to int
).
In 1979 C was defiled by Bjarne Stroustrup and turned into the abomination nowadays known as C++.
In 1989 the first standardized version of C was released, C89 also known as ANSI C, a standard that remains relevant today, being considered as a baseline for maximum portable C code.
The next major (and last good version) version, C99 was released in the year 2000. Notably this standard is the first one
that formally allowed the declaration of a variable in any place of a block, although most compilers supported it already
as an extension. Also adds C++ style //
comments, the stdint.h
(fixed integer types) header and a boolean type.
Currently there are multiple independent compilers (non exhaustive list):
- GCC (GNU compiler collection)
- Clang: based in LLVM.
- TCC (Tiny C Compiler): by Fabrice Bellard, a small compiler with fast compile times, implements modern C standards.
- chibicc: "toy" compiler by Rui Ueyama.
- SubC: educational compiler by Nils M Holm, public domain.
- cproc: uses the QBE backend.
- onramp: small C toolchain meant for bootstrapping.
- Kefir
- pcc (Portable C Compiler): classic portable compiler from the Unix days, successor of Ritchie's C compiler.
- OpenWatcom: a C compiler from the DOS days, proprietary.
- MSVC: proprietary compiler from Micro$oft, notable for being stuck in ANSI C for decades.
- ...and many more implementations
Examples
Hello world
#include <stdio.h>
int
main(void)
{
puts("Hello, world!");
return 0;
}
Compiling
On Unix systems, cc
is symlinked to the "default" C compiler, usually GCC in Linux and Clang in the BSDs.
cc -o hello hello.c
Factorial
Technically C99, because of the long long
.
#include <stdio.h> /* printf */
#include <stdlib.h> /* atoi */
long long /* a long long is 64 bits wide in Linux */
fact(int n)
{
long long res = 1;
while (n > 0) {
res *= n;
--n;
}
return res;
}
int
main(int argc, char **argv)
{
if (argc < 2)
return 1;
/* in "real" code you may want to use the strtol/strtoul functions */
int n = atoi(argv[1]);
printf("fact(%i) = %lli\n", n, fact(n));
return 0;
}
Resources
- The C Programming Language by Brian Kernighan and Dennis Ritchie, covers the ANSI C version.
- The Development of the C Language by Dennis Ritchie.