C
The C programming language reference - types, pointers, arrays, strings, structs, manual memory, the preprocessor, I/O, and modern C23 features.
Sign in to mark items as known and track your progress.
Sign inGetting Started
Your first program, compiling, and output.
Hello World
The entry point and printf.
// Quick Reference
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}Compile & Run
Build with warnings and a modern standard.
// Quick Reference
gcc -std=c23 -Wall main.c -o main
./mainVariables & Types
Fundamental types, sizes, and constants.
Types & sizeof
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 keywordConstants & Limits
const, 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.
Operators
The core operator set.
// Quick Reference
+ - * / % // arithmetic
== != < > <= >= // comparison
&& || ! // logical
& | ^ ~ << >> // bitwise
cond ? a : b // ternaryControl Flow
Branching and looping.
Conditionals & switch
if/else and switch.
// Quick Reference
if (x > 0) { ... } else if (x < 0) { ... } else { ... }
switch (n) {
case 1: ...; break;
default: ...;
}Loops
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.
Functions & Prototypes
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.
Pointer Basics
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)Function Pointers & void*
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.
Arrays
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.
C Strings
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.
Structs & typedef
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.
malloc, realloc & free
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.
printf, scanf & fgets
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.
File I/O
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.
Macros & Directives
#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.
static, extern, const & volatile
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.
Bitwise Tricks
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.
stdlib, math, ctype & time
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.
argc/argv, errno & assert
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.
C23 Highlights
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);