C Programming (C / C+)
Welcome to the C reference hub, covering the foundational systems language often nicknamed C+ in the community.
Keywords
- auto
- break
- case
- char
- const
- continue
- default
- do
- double
- else
- enum
- extern
- float
- for
- goto
- if
- inline
- int
- long
- register
- restrict
- return
- short
- signed
- sizeof
- static
- struct
- switch
- typedef
- union
- unsigned
- void
- volatile
- while
Core Concepts
- Translation units compiled into object files and linked.
- Header files declare functions (use header guards).
- Manual memory management through stack, heap, and static storage.
- Pointer arithmetic enables low-level data access.
- Undefined behavior must be avoided through disciplined coding.
- Use `const` to describe read-only data and interfaces.
- Separate interface (header) from implementation (source).
Data Types and Storage
- Integer family: char, short, int, long, long long (+ signed/unsigned).
- Floating family: float, double, long double.
- Derived types: pointers, arrays, structures, unions, function pointers.
- Storage classes: auto, register, static, extern.
- Type qualifiers: const, volatile, restrict, _Atomic (C11).
Operators
Arithmetic
Comparison
Logical
Bitwise
Assignment
- = += -= *= /= %= &= |= ^= <<= >>=
Pointer and Object
- * pointer dereference
- & address-of
- [] array subscription
- . member access
- -> pointer member access
- (type) cast operator
Standard Library Essentials
- <stdio.h>: printf, scanf, fgets, puts.
- <stdlib.h>: malloc, calloc, realloc, free, exit, qsort, bsearch.
- <string.h>: memcpy, memmove, memset, strlen, strcmp, strtok.
- <ctype.h>: character classification helpers.
- <time.h>: clock, time, difftime, struct tm.
- <assert.h>: assert macro guards invariants.
- <stdint.h>: fixed-width integer typedefs (uint32_t etc.).
Compilation Workflow
- Preprocessing: macros, includes, conditional compilation.
- Compilation: source to object with optimizations `-O0`..`-O3`.
- Linking: resolve symbols into executable or shared library.
- Toolchain examples: GCC, Clang, MSVC, TinyCC.
- Use `-Wall -Wextra -Werror` to surface issues early.
Best Practices
- Minimize globals; prefer dependency injection via parameters.
- Document ownership of pointers and expected lifetimes.
- Prefer `size_t` for sizes and array indices.
- Check every allocation and I/O result for errors.
- Leverage static analysis (clang-tidy, cppcheck) to catch UB.
- Adopt coding standards like MISRA-C or CERT C when shipping safety-critical software.