0 Introduction

A consolidated reference covering basic to advanced C syntax.

Scope & Purpose

C remains the lingua franca of systems-level programming—used for OS kernels, embedded systems, real-time DSP, and more. This guide follows C17/C18 with notes on C23.

Notation: keywords are in teal; function names in purple; parameters in orange.

1 Program Structure

Files, translation units, the main entry point.

1.1 Minimal Skeleton


#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello world\n");
    return 0;
}

1.2 Translation Phases (overview)

  1. Physical source file → character set mapping.
  2. Trigraph/line-splicing processing.
  3. Tokenization and macro expansion.
  4. Parsing, syntax/semantic analysis.
  5. Object code generation per translation unit.
  6. Link editor produces executable.

2 Data Types

Primitive, derived, and user-defined types.

2.1 Fundamental Types


// Exact width (C99 <stdint.h>)
int8_t    i8;
uint64_t  counter;

// Floating-point
float     f32;
double    f64;
long double f80;     // x86 extended (optional)

// Character
char      c;         // signed or unsigned implementation-defined
char16_t  utf16;     // C23 (new)

2.2 Qualifiers

  1. signed, unsigned
  2. short, long, long long
  3. const, volatile, restrict, _Atomic

2.3 Derived Types

  1. Pointers — T *
  2. Arrays — T name[size] (VLAs optional after C23)
  3. Functions — return_type (*fn)(param_list)

3 Variables & Constants

Declarations, definitions, initializers, literals.

3.1 Declarations vs Definitions

A declaration introduces a name; a definition allocates storage.


extern int  global;     // declaration (no storage)
int    global = 42;     // definition (storage allocated)

3.2 Initializers


int arr[4] = { [0] = 1, [3] = 5 };   // designated initializers (C99)
struct Vector v = {.x = 0.0f, .y = 1.0f};

4 Operators & Expressions

Precedence, associativity, evaluation.

4.1 Categories

  1. Arithmetic +, - , *, / , %
  2. Bit-wise &, |, ^, ~, <<, >>
  3. Logical &&, ||, !
  4. Relational <, >, <=, >=, ==, !=
  5. Assignment =, +=, -=, ... , >>=
  6. Other ?: (sizeof) (_Alignof)

4.2 Sequence Points (C17)

Guarantees that all side-effects of left operand are complete before the right.

5 Control Flow

Branching, looping, and jump statements.

5.1 Selection


if(condition) { ... } else if (...) { ... }

switch(value) {
    case 1:  /* fallthrough */ 
    case 2:  puts("one or two");  break;
    default: puts("other");
}

5.2 Iteration


for(size_t i = 0; i < n; ++i)   { /* ... */ }
while(expr)                      { /* ... */ }
do { /* body */ } while(expr);

5.3 Jumps

  1. break, continue
  2. goto + labels (use sparingly)
  3. return

6 Functions

Prototypes, parameters, inline, variadics.

6.1 Prototype and Definition


double hypot(double a, double b);   // prototype

double hypot(double a, double b)
{
    return sqrt(a*a + b*b);
}

6.2 Parameter Passing

  1. By value (default) → copies.
  2. Use pointers for by-reference semantics.

6.3 Variadic Functions


#include <stdarg.h>

double sum(size_t count, ...)
{
    va_list ap;
    va_start(ap, count);
    double s = 0.0;
    for(size_t i = 0; i < count; ++i)
        s += va_arg(ap, double);
    va_end(ap);
    return s;
}

7 Storage Class & Linkage

auto, static, extern, register, thread_local.

7.1 Automatic ( auto )

Default for block-scope variables—lifetime limited to block.

7.2 Static Duration


static int counter;     // internal linkage (translation unit scope)

7.3 Thread Storage (C11)


_Thread_local int tls_id;

8 Pointers

Indirection, arrays, pointer arithmetic, const correctness.

8.1 Declaration Patterns


int  *pi;          // pointer to int
int (*fp)(void);   // pointer to function returning int
int  *arr[10];     // array of 10 int* 
int (*mat)[10];    // pointer to array of 10 int

8.2 Pointer Arithmetic

Add/subtract integer multiples of sizeof(*p).

9 Arrays & Strings

Fixed-size, VLA, string literals, multidimensional arrays.

9.1 Initialization Rules


char msg[] = "C syntax";       // array size = 8 with NUL
int  mat[2][3] = {{1,2,3},{4,5,6}};

9.2 VLAs (optional after C23)

10 Structs, Unions, Enums

User-defined aggregate types.

10.1 Structures


struct Person {
    char  name[32];
    int   age;
};

10.2 Bit-fields


struct Flags {
    unsigned ready : 1;
    unsigned error : 1;
};

10.3 Unions

Share storage for different representations.

10.4 Enumerations


typedef enum { RED, GREEN, BLUE } Color;