A consolidated reference covering basic to advanced C syntax.
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.
Files, translation units, the main
entry point.
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world\n");
return 0;
}
Primitive, derived, and user-defined 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)
signed
, unsigned
short
, long
, long long
const
, volatile
, restrict
, _Atomic
T *
T name[size]
(VLAs optional after C23)return_type (*fn)(param_list)
Declarations, definitions, initializers, literals.
A declaration introduces a name; a definition allocates storage.
extern int global; // declaration (no storage)
int global = 42; // definition (storage allocated)
int arr[4] = { [0] = 1, [3] = 5 }; // designated initializers (C99)
struct Vector v = {.x = 0.0f, .y = 1.0f};
Precedence, associativity, evaluation.
+, - , *, / , %
&, |, ^, ~, <<, >>
&&, ||, !
<, >, <=, >=, ==, !=
=, +=, -=, ... , >>=
?: (sizeof) (_Alignof)
Guarantees that all side-effects of left operand are complete before the right.
Branching, looping, and jump statements.
if(condition) { ... } else if (...) { ... }
switch(value) {
case 1: /* fallthrough */
case 2: puts("one or two"); break;
default: puts("other");
}
for(size_t i = 0; i < n; ++i) { /* ... */ }
while(expr) { /* ... */ }
do { /* body */ } while(expr);
break
, continue
goto
+ labels (use sparingly)return
Prototypes, parameters, inline, variadics.
double hypot(double a, double b); // prototype
double hypot(double a, double b)
{
return sqrt(a*a + b*b);
}
#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;
}
auto
, static
, extern
, register
, thread_local
.
auto
)Default for block-scope variables—lifetime limited to block.
static int counter; // internal linkage (translation unit scope)
_Thread_local int tls_id;
Indirection, arrays, pointer arithmetic, const correctness.
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
Add/subtract integer multiples of sizeof(*p)
.
Fixed-size, VLA, string literals, multidimensional arrays.
char msg[] = "C syntax"; // array size = 8 with NUL
int mat[2][3] = {{1,2,3},{4,5,6}};
User-defined aggregate types.
struct Person {
char name[32];
int age;
};
struct Flags {
unsigned ready : 1;
unsigned error : 1;
};
Share storage for different representations.
typedef enum { RED, GREEN, BLUE } Color;