C Cheatsheet
This page is a quick reference for C patterns that show up constantly in DSA, competitive programming, and systems programming. If you're just starting out, don't worry - every snippet here is explained line by line 😊
Video Explanation

Basic Syntax
Data Types
Basic data type syntax in C
int a = 1; // 32-bit integer
long long b = 1000000000LL; // 64-bit integer
float f = 3.14f; // 32-bit decimal
double d = 3.14159; // 64-bit decimal
char c = 'A'; // 8-bit character (also used as small integer)
unsigned int u = 42; // Non-negative 32-bit integer
short s = 32767; // 16-bit integer
Format Specifiers (printf / scanf)
Common format specifiers in C
printf("%d", a); // int
printf("%lld", b); // long long
printf("%f", d); // float or double
printf("%c", c); // char
printf("%s", str); // string (char array)
printf("%u", u); // unsigned int
printf("%x", a); // hexadecimal (lowercase)
printf("%p", ptr); // pointer address
scanf("%d", &a); // Read int - note the & (address-of)
scanf("%lld", &b); // Read long long
scanf("%s", str); // Read string - no & needed for arrays
Operators and Control Flow
Control flow syntax in C
// if, else if, else
if (a > 0) {
// ...
} else if (a == 0) {
// ...
} else {
// ...
}
for (int i = 0; i < n; i++) {} // i -> 0, 1, 2, ..., n-1
while (n-- > 0) {} // n -> n, n-1, ..., 1
do { /* runs at least once */ } while (condition);
switch (a) {
case 1: /* ... */ break;
default: /* ... */
}
Bitwise Operators
Bitwise operators in C
int x = 5; // Binary: 0101
int y = 3; // Binary: 0011
int and = x & y; // 0001 = 1 — both bits must be 1
int or = x | y; // 0111 = 7 — at least one bit is 1
int xor = x ^ y; // 0110 = 6 — exactly one bit is 1
int not = ~x; // Flips all bits
int lsh = x << 1; // 1010 = 10 — multiply by 2
int rsh = x >> 1; // 0010 = 2 — divide by 2
// Common tricks
int isEven = !(x & 1); // true if x is even
int setBit = x | (1 << 2); // Set bit 2
int clrBit = x & ~(1 << 2); // Clear bit 2
int togBit = x ^ (1 << 2); // Toggle bit 2
int chkBit = (x >> 2) & 1; // Check bit 2
Input and Output
I/O in C
#include <stdio.h>
int n;
scanf("%d", &n); // Read a single integer
printf("Value: %d\n", n); // Print integer with newline
char line[100];
fgets(line, sizeof(line), stdin); // Read full line including spaces
// Fast I/O for competitive programming
#define FAST_IO() // just use scanf/printf — already faster than cin/cout
Arrays
Array syntax in C
int arr[5] = {1, 2, 3, 4, 5}; // 1D array, size 5, values initialized
int zeros[100] = {0}; // All elements set to 0
int grid[3][4]; // 2D array, 3 rows, 4 columns
// Initializing with memset (from string.h)
memset(arr, 0, sizeof(arr)); // Set all bytes to 0
memset(arr, -1, sizeof(arr)); // Set all bytes to 0xFF, so each int becomes -1
int len = sizeof(arr) / sizeof(arr[0]); // Number of elements in arr, i.e len = 5
Strings
String syntax in C
#include <string.h>
char s[50] = "hello"; // String is a char array ending with '\0'
int len = strlen(s); // Length of string, i.e len = 5 (excludes '\0')
strcpy(dest, s); // Copy s into dest
strcat(dest, s); // Append s to dest
int cmp = strcmp(s1, s2); // 0 if equal, <0 if s1 < s2, >0 if s1 > s2
char *pos = strstr(s, "ell"); // Pointer to first occurrence of "ell" in s, or NULL
// Character checks (from ctype.h)
#include <ctype.h>
isdigit('3'); // true — is it a digit?
isalpha('A'); // true — is it a letter?
islower('a'); // true — is it lowercase?
toupper('a'); // Returns 'A'
tolower('A'); // Returns 'a'
Pointers
Pointer syntax in C
int x = 10;
int *ptr = &x; // ptr holds the address of x
int val = *ptr; // Dereference — val = 10 (value at address ptr)
*ptr = 20; // Modify x through ptr — now x = 20
// NULL pointer
int *p = NULL; // Safe default for uninitialized pointers
if (p != NULL) {} // Always check before dereferencing
// Pointer to pointer
int **pp = &ptr; // pp holds address of ptr
int v = **pp; // Double dereference — v = 20
Pointer Arithmetic
Pointer arithmetic in C
int arr[] = {10, 20, 30, 40};
int *p = arr; // p points to arr[0]
p++; // p now points to arr[1]
int x = *(p + 2); // x = arr[3] = 40 — offset by 2 from current position
int diff = p - arr; // Number of elements between pointers, i.e diff = 1
// Array and pointer equivalence
arr[i] == *(arr + i); // These are identical in C
void* and Type Casting
void pointer and casting in C
void *vp; // Generic pointer — can hold any type's address
int x = 5;
vp = &x; // Assign int address to void pointer
int *ip = (int *)vp; // Cast back to int pointer before dereferencing
int val = *ip; // val = 5
// Casting in general
double d = (double)5 / 2; // d = 2.5 — cast before division
int i = (int)3.99; // i = 3 — truncates decimal
Functions
Function syntax in C
// Declaration (prototype) — needed if defined after main
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b; // Returns sum of a and b
}
// Call
int result = add(3, 4); // result = 7
// Void function
void greet(char *name) {
printf("Hello, %s\n", name); // No return value
}
Pass by Value vs Pass by Pointer
Pass by value and pointer in C
void doubleVal(int x) {
x *= 2; // Only modifies local copy — caller unchanged
}
void doublePtr(int *x) {
*x *= 2; // Modifies the original variable through pointer
}
int a = 5;
doubleVal(a); // a still = 5
doublePtr(&a); // a now = 10
Recursion
Recursion syntax in C
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int fib(int n) {
if (n <= 1) return n; // Base case: fib(0)=0, fib(1)=1
return fib(n - 1) + fib(n - 2); // Recursive case
}
Structures
Struct syntax in C
struct Point {
int x;
int y;
};
struct Point p = {3, 4}; // Initialize struct
p.x = 10; // Access member with dot operator
printf("%d %d\n", p.x, p.y);
// typedef — avoid writing 'struct' every time
typedef struct {
int x, y;
} Point;
Point q = {1, 2}; // No 'struct' keyword needed
Struct with Pointer
Struct pointer syntax in C
Point *ptr = &q;
ptr->x = 99; // Arrow operator — dereference and access field
// Equivalent to: (*ptr).x = 99
Nested Struct
Nested struct in C
typedef struct {
Point top_left;
Point bottom_right;
} Rect;
Rect r = {{0, 0}, {10, 5}};
printf("%d\n", r.top_left.x); // Access nested field