C Cheatsheet
A dense, scannable quick reference for core C syntax and standard library usage.
Data Types & Format Specifiers
C
char c; // %c -- 1 byte short s; // %hd -- >= 2 bytes int i; // %d -- >= 2 bytes (usually 4) long l; // %ld -- >= 4 bytes long long ll; // %lld -- >= 8 bytes unsigned int u; // %u float f; // %f double d; // %f (printf), %lf (scanf) size_t sz; // %zu void *p; // %p #include <stdint.h> int8_t i8; uint8_t u8; int16_t i16; uint16_t u16; int32_t i32; uint32_t u32; int64_t i64; uint64_t u64;
Operators
C
// Arithmetic: + - * / % // Relational: == != < > <= >= // Logical: && || ! // Bitwise: & | ^ ~ << >> // Assignment: = += -= *= /= %= &= |= ^= <<= >>= // Increment: ++i (pre) i++ (post) // Ternary: condition ? value_if_true : value_if_false // sizeof: sizeof(x) or sizeof(type) // Comma: expr1, expr2 (evaluates left to right, result is expr2)
Control Flow
C
if (cond) { } else if (cond2) { } else { }
switch (x) {
case 1: /* ... */ break;
case 2: /* ... */ break;
default: /* ... */
}
for (int i = 0; i < n; i++) { }
while (cond) { }
do { } while (cond);
break; // exit loop/switch
continue; // skip to next iteration
goto label; label: ;Functions
C
// Declaration (prototype) -- usually in a header
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b;
}
// Function pointer
int (*op)(int, int) = add;
int result = op(2, 3);
// Variadic function
#include <stdarg.h>
int sum(int count, ...) {
va_list args;
va_start(args, count);
int total = 0;
for (int i = 0; i < count; i++) total += va_arg(args, int);
va_end(args);
return total;
}Pointers
C
int x = 5; int *p = &x; // p holds the address of x int y = *p; // dereference: y = 5 int *const cp = &x; // constant pointer to int const int *pc = &x; // pointer to constant int const int *const cpc = &x; // constant pointer to constant int int **pp = &p; // pointer to pointer void *vp; // generic pointer -- must be cast before dereferencing int *np = NULL; // null pointer -- always check before dereferencing
Arrays & Strings
C
int arr[5] = {1, 2, 3, 4, 5};
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
size_t n = sizeof(arr) / sizeof(arr[0]);
char str[] = "hello"; // 6 bytes, includes '\0'
char *s = "hello"; // pointer to a string literal (read-only)
#include <string.h>
strlen(s); strcpy(dst, src); strncpy(dst, src, n);
strcat(dst, src); strncat(dst, src, n); strcmp(a, b);
strncmp(a, b, n); strchr(s, 'l'); strstr(s, "ell");
snprintf(buf, sizeof(buf), "%d", 42); // safe formatted writeStructs
C
struct point {
int x;
int y;
};
struct point p1 = {1, 2};
struct point *pp = &p1;
pp->x = 10; // equivalent to (*pp).x = 10;
typedef struct {
int x;
int y;
} point_t; // usable as "point_t" without the "struct" keyword
union number {
int i;
float f;
}; // members share the same memory -- sizeof = size of largest memberDynamic Memory
C
#include <stdlib.h>
int *a = malloc(n * sizeof(int)); // uninitialized memory
int *b = calloc(n, sizeof(int)); // zero-initialized memory
int *c = realloc(a, new_n * sizeof(int)); // resize (may move the block)
if (a == NULL) { /* handle allocation failure */ }
free(a); // release memory
a = NULL; // avoid leaving a dangling pointer aroundPreprocessor
C
#include <stdio.h> // system header
#include "myheader.h" // local header
#define PI 3.14159 // object-like macro
#define SQUARE(x) ((x) * (x)) // function-like macro
#ifndef HEADER_H
#define HEADER_H
// ... header contents ...
#endif
#pragma once // simpler alternative to the guard above (widely supported)
#ifdef DEBUG
printf("debug info\n");
#endif
#if defined(__linux__)
// Linux-specific code
#elif defined(_WIN32)
// Windows-specific code
#endif