C is a variously licensed self-hosted programming language.
- Website
- GitHub
- AlternativeTo
> QUOTE
# Notability
It is the language which underlies most operating systems and other languages and more system software has been written in it than anything else.
# Philosophy
The C Language Specification guides the various implementations out there, with many of them conforming to one version of the standard or another, typically with their own extensions.
The set of GCC extensions is possibly the largest and many other C compilers attempt to match many of those additional features.
# Target Support
Everything.
## Implementations
- Clang - LLVM
- GCC - GNU
- TCC
- VisualStudio - Microsoft
- Intel
# Features
## C23
- `defer` support - <https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Improved%20__attribute__((cleanup))%20Through%20defer.html>
- `auto` type "deduction" - https://stackoverflow.com/questions/76248851/c23-auto-vs-c11-auto
- https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu
## C99
The version of C standardized in [[1999]]. Many libraries still are designed to accommodate this standard.
# Tips
## Print Bits
### ISO C
This feature was added in [[glibc]] 2.35 in [[2022]] based on the ISO draft.
> printf-family functions now support the %b format for output of integers in binary, as specified in draft ISO C2X
```c
printf("%b", x);
```
```c
int x = 1 << 1;
printf("%08b", x); // 00000010
```
### Simple
```c
void printbits(unsigned char v) {
int i; // for C89 compatability
for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
```
### Any Datatype
```c
// Assumes little endian
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i = size-1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}
```
```c
int main(int argc, char* argv[])
{
int i = 23;
uint ui = UINT_MAX;
float f = 23.45f;
printBits(sizeof(i), &i);
printBits(sizeof(ui), &ui);
printBits(sizeof(f), &f);
return 0;
}
```
### Table-Based
```c
const char *bit_rep[16] = {
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
};
void print_byte(uint8_t byte)
{
printf("%s%s", bit_rep[byte >> 4], bit_rep[byte & 0x0F]);
}
```
## Cursed C
- https://cohost.org/easrng/post/6848829-empty#comments
## The \_Generic Keyword
`_Generic` is a variadic keyword that evaluates the initial parameter at compile time and the other parameters are key/value pairs with the key being the type of the first expression and the value being another expression that is inserted at compile time.
```c
#define print_value(v) _Generic(v, float: printf("%f", v), int: printf("%d", v), default: printf("anything else"))
```
```c
#define CONSTEXPR_IF(condition, then, otherwise) \
_Generic(&(char[1 + !!(EXPR)]){0}, \
char(*)[2]: (then), \
char(*)[1]: (otherwise))
```
- https://thatonegamedev.com/cpp/writing-generic-code-in-c/
# References
- https://clang.llvm.org/docs/DiagnosticsReference.html
- https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
- https://hackaday.com/2018/03/02/unionize-your-variables-an-introduction-to-advanced-data-types-in-c/
- https://stackoverflow.com/questions/2963898/faster-alternative-to-memcpy
- https://stackoverflow.com/questions/18857056/c11-generic-how-to-deal-with-string-literals
- https://stackoverflow.com/questions/8876043/multi-threading-support-in-c11
- https://stackoverflow.com/questions/699968/display-the-binary-representation-of-a-number-in-c
- https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format