Learn Cheatsheet

C / C++

Core C and C++ syntax at a glance.

Basic Types
int n = 0; 32-bit signed integer
float f = 1.5f; 32-bit floating point
double d = 3.14; 64-bit floating point
char c = 65; Single byte — ASCII value
unsigned int u = 0; Non-negative integer
long long big = 0; 64-bit integer
Pointers & References
int* ptr = &x; Pointer: stores address of x
*ptr = 5; Dereference: write through pointer
ptr->member Access member via pointer
int& ref = x; C++ reference: alias (cannot be null)
const int* ptr Pointer to constant data
int* const ptr Constant pointer (address is fixed)
Control Flow
if (cond) { } else { } Conditional branch
for (int i=0; i<n; i++) { } Classic for loop
while (cond) { } Pre-condition loop
do { } while (cond); Post-condition (runs at least once)
switch (x) { case 1: break; } Multi-branch on integer/char
Functions
int add(int a, int b) Function declaration
void fn(int* p) Pass by pointer — C style
void fn(int& r) Pass by reference — C++
inline int sq(int x) Inline hint to compiler
int fn(int x = 0) Default parameter — C++
Memory Management
malloc(n * sizeof(T)) Allocate n items on heap — C
free(ptr) Release C heap memory
new T Allocate single object — C++
new T[n] Allocate array — C++
delete ptr / delete[] ptr Release C++ heap memory