less_retarded_wiki/c.md

49 lines
3.4 KiB
Markdown
Raw Normal View History

2021-11-03 21:47:50 +01:00
# C
2021-11-04 02:59:55 +01:00
C is a low-level, statically typed imperative compiled language, the go-to language of most [less retarded software](lrs.md). It is the absolutely preferred language of the [suckless](suckless.md) community as well as of most true experts, for example the [Linux](linux.md) and [OpenBSD](openbsd.md) developers, because of its good minimal design, level of control, uncontested performance and a greatly established and tested status.
2021-11-03 21:47:50 +01:00
2021-11-04 02:59:55 +01:00
C is usually not considered an easy language to learn because of its low level nature: it requires good understanding of how a computer actually works and doesn't prevent the programmer from shooting himself in the foot. Programmer is given full control (and therefore responsibility). There are things considered "tricky" which one must be aware of, such as undefined behavior of certain operators and raw pointers. This is what can discourage a lot of modern "coding monkeys" from choosing C, but it's also what inevitably allows such great performance -- undefined behavior allows the compiler to choose the most efficient implementation.
2021-11-03 21:47:50 +01:00
# History and Context
# Standards
C is not a single language, there have been a few standards over the years since its inception in 1970s. The notable standards and versions are:
- K&R C: C as described by its inventors in the book *The C Programming Language*, before official standardization. This is kind of too ancient nowadays.
- C89/C90 (ANSI/ISO C): First fully standardized version, usable even today.
- C95: A minor update of the previous standard, adds wide character support.
- C99: Updated standard from the year 1999 striking a great balance between "modern" and "good old". This is a good version to use in LRS programs, but will be a little less supported than C89.
- C11: Updated standard from the year 2011. This one is too bloated and isn't worth using.
- C17/C18: Yet another update, yet more bloated and not worth using.
LRS should use C99 or C89 as the newer versions are considered [bloat](bloat.md) and don't have such great support in compilers, making them less portable and therefore less free.
The standards of C99 and older are considered pretty [future-proof](future_proof.md) and using them will help your program be future-proof as well. This is to a high degree due to C having been established and tested better than any other language; it is one of the oldest languages and a majority of the most essential software is written in C, C compiler is one of the very first things a new hardware platform needs to implement, so C compilers will always be around, at least for historical reasons. C has also been very well designed in a relatively minimal fashion, before the advent of modern feature-creep and and bullshit such as [OOP](oop.md) which cripples almost all "modern" languages.
# Compilers
- gcc
- clang
- tcc
# Basics
A simple program in C looks like e.g. like this:
```
TODO
```
You can quickly compile the program from command line like this:
`gcc -o my_program my_program.c`
You can replace `gcc` with other compilers (e.g. `clang`, `tcc`, `g++` etc.), they mostly understand the same flags. Some important flags you may want to add:
- `-O3`: optimize for program speed, greatly speeds up your program (you can also use less aggressive `-O2` and `-O1`)
- `-Os`: optimize for smaller program size
- `-g`: include debug info, you want this so that debuggers can point to your source code
- `-std=c99`: use the C99 standard