C
The C programming language reference - types, pointers, arrays, strings, structs, manual memory, the preprocessor, I/O, and modern C23 features.
Getting Started
Your first program, compiling, and output.
The entry point and printf.
// Quick Reference
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}Build with warnings and a modern standard.
// Quick Reference
gcc -std=c23 -Wall main.c -o main
./mainVariables & Types
Fundamental types, sizes, and constants.
C's built-in types and their sizes.
// Quick Reference
int i = 42;
long l = 42L;
double d = 3.14;
char c = 'A';
unsigned u = 10;
bool ok = true; // C23 keywordconst, enums, and type limits.
// Quick Reference
const double PI = 3.14159;
#define MAX 100
#include <limits.h>
INT_MAX; // largest intOperators
Arithmetic, comparison, logical, and bitwise.
The core operator set.
// Quick Reference
+ - * / % // arithmetic
== != < > <= >= // comparison
&& || ! // logical
& | ^ ~ << >> // bitwise
cond ? a : b // ternaryControl Flow
Branching and looping.
if/else and switch.
// Quick Reference
if (x > 0) { ... } else if (x < 0) { ... } else { ... }
switch (n) {
case 1: ...; break;
default: ...;
}for, while, do-while, and jumps.
// Quick Reference
for (int i = 0; i < 5; i++) { ... }
while (cond) { ... }
do { ... } while (cond);
break; continue;Functions
Declaring, calling, and scoping behavior.
Definitions, prototypes, and recursion.
// Quick Reference
int add(int a, int b); // prototype (declaration)
int add(int a, int b) { // definition
return a + b;
}Pointers
C's defining feature - addresses and indirection.
Address-of, dereference, and arithmetic.
// Quick Reference
int n = 5;
int *p = &n; // p holds the address of n
*p = 10; // dereference: n is now 10
int *q = nullptr; // C23 (or NULL)Callbacks and generic pointers.
// Quick Reference
int (*op)(int, int) = &add; // function pointer
int r = op(2, 3);
void *generic; // points to anythingArrays
Contiguous fixed-size storage.
Declaration, iteration, and the pointer relationship.
// Quick Reference
int a[5] = {1, 2, 3}; // rest are 0
int n = sizeof(a) / sizeof(a[0]); // length
int grid[2][3]; // 2DStrings
Null-terminated char arrays and string.h.
char arrays and the string library.
// Quick Reference
char s[] = "hello"; // char array + '\0'
#include <string.h>
strlen(s); strcmp(a, b);
strcpy(dst, src); snprintf(buf, n, "%d", x);Structs, Unions & Enums
Grouping and naming data.
Composite types and member access.
// Quick Reference
struct Point { int x, y; };
struct Point p = {3, 4};
p.x; // dot for a value
ptr->x; // arrow for a pointerDynamic Memory
Heap allocation you manage yourself.
Allocate, resize, and release memory.
// Quick Reference
#include <stdlib.h>
int *p = malloc(n * sizeof *p);
if (!p) { /* handle */ }
free(p);
p = nullptr;Input & Output
Formatted console I/O.
Format specifiers and reading input.
// Quick Reference
printf("%d %.2f %s\n", 42, 3.14, "hi");
scanf("%d", &n);
fgets(buf, sizeof buf, stdin);File I/O
Reading and writing files.
fopen, fgets, fprintf, and fclose.
// Quick Reference
FILE *f = fopen("data.txt", "r");
if (!f) { perror("open"); }
fgets(buf, sizeof buf, f);
fclose(f);Preprocessor
Text transformation before compilation.
#define, #include, and conditional compilation.
// Quick Reference
#define MAX 100
#define SQUARE(x) ((x) * (x))
#include <stdio.h>
#ifndef HEADER_H
#define HEADER_H
#endifStorage Classes & Qualifiers
Lifetime, linkage, and type qualifiers.
Control storage duration and access.
// Quick Reference
static int counter; // file scope OR persistent local
extern int shared; // defined in another file
const int MAX = 100; // read-only
volatile int reg; // do not optimize accessesBit Manipulation
Working at the bit level.
Masks, flags, and shifts.
// Quick Reference
x & mask // test bits
x | flag // set bits
x & ~flag // clear bits
x ^ flag // toggle bits
x << n // shift leftStandard Library
Essential headers beyond stdio.
Common utility functions.
// Quick Reference
#include <stdlib.h> // atoi, qsort, rand, exit
#include <math.h> // sqrt, pow, floor
#include <ctype.h> // isdigit, toupper
#include <time.h> // time, clockCommand-line & Error Handling
Program arguments and reporting failures.
Read arguments and handle errors.
// Quick Reference
int main(int argc, char *argv[]) {
// argv[0] = program name, argv[1..] = args
}
#include <errno.h>
perror("open failed");
assert(x > 0);Modern C (C23)
Features from today's standard.
Keywords and features new to C23.
// Quick Reference
bool ok = true; // keywords (no <stdbool.h>)
int *p = nullptr; // typed null
constexpr int SIZE = 10; // compile-time constant
[[nodiscard]] int f(void);