C Language Overview

C (ANSI C99 / C11) — systems language | minimal runtime | manual memory | maximal control

Paradigm

Imperative, procedural. Static typing with weak type system (implicit conversions, void*). Compilation via preprocessor → compiler → assembler → linker. One-pass compilation, translation-unit-based.

Types

Category Types Size (typical)
char char 1 byte
Signed int signed char short int long long long 1, 2, 4, 8, 8 bytes
Unsigned int unsigned char unsigned short unsigned int unsigned long unsigned long long 1, 2, 4, 8, 8 bytes
Float float double long double 4, 8, 8-16 bytes
Bool (C99) _Bool / bool (via <stdbool.h>) 1 byte
Void void 0 (incomplete)
Pointer T* 8 bytes (64-bit)
Array T[N] T[] N * sizeof(T)
Struct struct Name { ... } Sum of fields + padding
Union union Name { ... } Max of fields
Enum enum Name { ... } sizeof(int)
Function pointer T (*)(args) 8 bytes

Type Modifiers

const volatile restrict (C99) inline (C99)

Storage Class Specifiers

auto register static extern _Thread_local (C11)

Declarations

Construct Syntax Notes
Function T name(params) { body }
Prototype T name(params); Declaration without body
Variable T x = expr;
Multiple T x, y, z;
Constant const T x = expr;
Enum constant enum { VAL = expr };
Typedef typedef T Alias;
Struct struct Name { T field; };
Union union Name { T field; };
Extern extern T name; Defined in another TU
Static static T name; Internal linkage

Control Flow

Construct Syntax Notes
If if (cond) { ... } else { ... } Statement, not expression
While while (cond) { ... }
Do-while do { ... } while (cond);
For for (init; cond; incr) { ... }
Switch switch (expr) { case N: ... break; } Fall-through by default
Break break; Exit loop/switch
Continue continue; Skip to next iteration
Goto goto label; Within function only
Return return expr;

Operators (precedence, highest to lowest)

Precedence Operators Assoc
1 () [] . -> ++ -- (postfix) Left
2 ++ -- (prefix) + - ! ~ * & sizeof _Alignof Right
3 (type) (cast) Right
4 * / % Left
5 + - Left
6 << >> Left
7 < > <= >= Left
8 == != Left
9 & Left
10 ^ Left
11 | Left
12 && Left
13 || Left
14 ? : (ternary) Right
15 = += -= *= /= %= &= ^= |= <<= >>= Right
16 , (comma operator) Left

Preprocessor

Directive Purpose
#include "file" / <file> Textual inclusion
#define MACRO body Object-like macro
#define MACRO(args) body Function-like macro
#undef MACRO Remove macro
#if expr / #ifdef MACRO / #ifndef MACRO Conditional compilation
#elif expr / #else / #endif Else branches
#error "message" Compile-time error
#pragma Implementation-defined
#line N "file" Line control
## (token pasting) Concatenate tokens
# (stringify) Convert to string literal

Predefined Macros (C99)

__LINE__ __FILE__ __DATE__ __TIME__ __STDC__ __STDC_VERSION__ __func__

Memory

Operation Syntax Header
Allocate malloc(n) <stdlib.h>
Aligned alloc (C11) aligned_alloc(a, n) <stdlib.h>
Calloc calloc(n, size) <stdlib.h>
Realloc realloc(p, n) <stdlib.h>
Free free(p) <stdlib.h>
Copy memcpy(dst, src, n) <string.h>
Move (overlap-safe) memmove(dst, src, n) <string.h>
Set memset(p, c, n) <string.h>
Compare memcmp(a, b, n) <string.h>
Stack alloca(n) (non-standard) <alloca.h>

Standard Library (selected)

<stdio.h>

printf scanf fopen fclose fread fwrite fprintf sprintf fgets fputs feof ferror remove rename tmpfile

<stdlib.h>

atoi atof strtol strtod rand srand abs qsort bsearch system getenv exit abort atexit

<string.h>

strlen strcpy strncpy strcat strncat strcmp strncmp strchr strrchr strstr strtok memset memcpy memmove memcmp

<math.h>

sin cos tan asin acos atan atan2 sqrt pow exp log log10 fabs ceil floor fmod fma

<time.h>

time clock difftime mktime strftime localtime gmtime

<assert.h>

assert(expr)

<stdint.h> (C99)

int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t intptr_t uintptr_t size_t

<stdbool.h> (C99)

bool true false

Compilation

Step Command Output
Preprocess cpp file.c > file.i Expanded source
Compile cc -S file.c Assembly (file.s)
Assemble cc -c file.c Object (file.o)
Link cc file.o -o prog Executable (prog)
All-in-one cc file.c -o prog Direct executable

Common Flags

-O0 -O1 -O2 -O3 -Os -g -Wall -Wextra -Wpedantic -std=c99 -std=c11 -DNAME=val -Ipath -Lpath -llib

Toolchain

GCC (GNU), Clang/LLVM, MSVC (Windows), TinyCC, CompCert

Storage Duration

Duration Allocation Lifetime
Automatic Stack Scope
Static Data segment Program lifetime
Thread-local (C11) TLS Thread lifetime
Allocated Heap (malloc/free) Manual

Linkage

Keyword Linkage Visibility
(none, file scope) External Entire program
static (file scope) Internal Current translation unit
extern External Entire program
static inline (C99) Internal Current TU (no out-of-line)

Undefined Behavior (selected pitfalls)

C99 vs C11 vs C17 vs C23

Standard Key additions
C89/ANSI C Original standard
C99 // comments, stdint.h, bool, restrict, inline, variadic macros, designated initializers, compound literals, for-scope declarations
C11 _Generic, _Static_assert, _Alignas, _Alignof, _Atomic, _Thread_local, anonymous structs/unions, noreturn, bounds-checked interfaces (Annex K)
C17 Bug-fix release (no new features)
C23 (draft) typeof, nullptr, #embed, bool as keyword, static_assert without _, constexpr, auto for type inference

Common C idioms

Opaque pointer (encapsulation)

// header
typedef struct Handle Handle;
Handle* handle_create(void);
void handle_destroy(Handle*);

// implementation
struct Handle { int fd; };

Callback (function pointer)

void sort(int* arr, int n, int (*cmp)(int, int));

Object-oriented (manual vtable)

struct Vtable { void (*draw)(void*); };
struct Shape { struct Vtable* vtable; };

Generic (void* + function pointer)

void qsort(void* base, size_t n, size_t size,
           int (*cmp)(const void*, const void*));