107 lines
4.9 KiB
Markdown
107 lines
4.9 KiB
Markdown
# C
|
|
C is a procedural, compiled [programming language](programming_language.md) created by Dennis Ritchie in 1973 for use in the Unix operating system.
|
|
Despite being an [old](old.md) 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](minimalism.md) 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](assembly.md), effectively
|
|
turning C into a portable assembly. [Detractors](rust.md) 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](cpu.md) 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](unix.md) 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++](cpp.md).
|
|
|
|
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](llvm.md).
|
|
- [TCC](tcc.md) (Tiny C Compiler): by Fabrice Bellard, a small compiler with fast compile times, implements modern C standards.
|
|
- [chibicc](https://github.com/rui314/chibicc): "toy" compiler by Rui Ueyama.
|
|
- [SubC](https://www.t3x.org/reload/index.html): educational compiler by Nils M Holm, public domain.
|
|
- [cproc](https://sr.ht/~mcf/cproc/): uses the [QBE](qbe.md) backend.
|
|
- [onramp](https://github.com/ludocode/onramp): small C toolchain meant for [bootstrapping](bootstrapping.md).
|
|
- [Kefir](https://kefir.protopopov.lv/)
|
|
- [pcc](https://web.archive.org/web/20231212090621/http://pcc.ludd.ltu.se/)
|
|
(Portable C Compiler): classic portable compiler from the Unix days, successor of Ritchie's C compiler.
|
|
- [OpenWatcom](https://openwatcom.org/): a C compiler from the DOS days, proprietary.
|
|
- MSVC: proprietary compiler from [Micro$oft](microsoft.md), notable for being stuck in ANSI C for decades.
|
|
- ...and many more implementations
|
|
|
|
## Examples
|
|
### Hello world
|
|
```c
|
|
#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`.
|
|
|
|
```c
|
|
#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*](http://9p.io/cm/cs/who/dmr/chist.html) by Dennis Ritchie.
|
|
|
|
## See also
|
|
- [C tutorial](c_tutorial.md)
|
|
- [Pointer](pointer.md)
|