# C standard library **C standard library** is one library that's always linked by default when you compile C programs. To use the library, a set of headers is offered which you can include by default. For example, `` and `` in this code are headers from the C standard library (but there are also headers which are *not* part of C standard library!). - `` provides a definition of the constant `EXIT_SUCCESS` - `` provides a declaration to the function `printf()` ```c // Compile: gcc main.c -o program // Run: ./program #include #include int main() { printf("Hello world!"); return EXIT_SUCCESS; } ``` ## Headers - [[assert.h]] - [[complex.h]] - [[ctype.h]] - [[errno.h]] - [[fenv.h]] - [[float.h]] - [[inttypes.h]] - [[iso646.h]] - [[limits.h]] - [[locale.h]] - [[math.h]] - [[setjmp.h]] - [[signal.h]] - [[stdalign.h]] - [[stdarg.h]] - [[stdatomic.h]] - [[stdbit.h]] - [[stdbool.h]] - [[stdckdint.h]] - [[stddef.h]] - [[stdint.h]] - [[stdio.h]] - [[stdlib.h]] - [[stdmchar.h]] - [[stdnoreturn.h]] - [[string.h]] - [[tgmath.h]] - [[thread.h]] - [[time.h]] - [[uchar.h]] - [[wchar.h]] - [[wctype.h]] ## What are headers for? Headers commonly define various constants (`#define`) e.g. `` defines `EXIT_SUCCESS` ```bash $ grep -e EXIT_SUCCESS < /usr/include/stdlib.h #define EXIT_SUCCESS 0 /* Successful exit status. */ ``` They also commonly declare various function calls e.g. `` declares `printf()` ```bash $ grep -e 'extern int printf' < /usr/include/ extern int printf (const char *__restrict __format, ...); ``` And they also declare various `struct`s, `union`s, and `typedef`s ## Paths All headers all live in `/usr/include` directory and have the file extension ".h" -- which stands for "header". For example - `/usr/include/stdio.h` The library itself lives in `/usr/lib`. It actually comes in two flavors: - `/usr/lib/libc.a` - Static library (.a stands for "archive") - `/usr/lib/libc.so` - Dynamic library (.so stands for "shared object") ## Static vs dynamic ## Austerity Compared to standard libraries of modern programming languages -- such as Rust, C++17, Python, JavaScript etc. -- you'll find the C standard library frustratingly barren and austere. It contains almost nothing! Furthermore, many parts of the standard library are either dangerous (`gets()`), confusing (`strtok()`), near-duplicates (`getc()` and `fgetc()`), or not actually part of thes standard at all (`itoa()`). ## What's missing? ### No data structures No maps. No vectors. No lists or trees. Nothing. If you want something like C++'s `std::vector` in C -- i.e. a dynamically grown array -- you'll have to go ahead make a `struct` for it and implement it yourself, with `calloc()`, `realloc()` and `free()`! ``` typedef struct _vector_t { void* v_data; size_t v_capacity; size_t v_size; } vector_t; ``` But many data structures common in higher programming languages -- like C++'s `std::map` or Python's dict `{a: 1, b: 2}` -- are internally implemented through non-trivial data structures like B-trees or Red-black trees. While C might be conceptually the fastest programming language in existence (you're a hair away from writing machine code!) if you don't bother to implement fast data structures you won't see that. ### No: string manipulation ### No: serialization No JSON. No YAML. No XML. No ini files. Nothing. ### No: filesystem No directory traversal. No file permission. ### No: web No sockets. No web servers. No requests. Nothing. ### No: unicode No emojis. No CJK, arabic. ### No: async or multithreading ## Hosted vs freestanding ## Links - https://en.cppreference.com/w/c/header.html - https://sourceware.org/glibc/manual/latest/html_mono/libc.html - https://sourceware.org/glibc/manual/latest/html_mono/libc.html