Skip to content
Home
Cheat Sheets
C
C Cheat Sheet
Learn C from the basics →
Variables & Types
int x = 5; double d = 3.14; char c = 'A';
Basic types
const int MAX = 100;
Constant (cannot be reassigned)
#define MAX 100
Preprocessor constant (text substitution)
unsigned int, long, short
Type modifiers
Control Flow
if (x > 0) { } else { }
Conditional
for (int i = 0; i < 10; i++) { }
Counting loop
while (condition) { }
While loop
switch (x) { case 1: break; default: break; }
Switch statement
Pointers & Arrays
int *p = &x;
Pointer to x
*p = 10;
Dereference — write through the pointer
int arr[5] = {1, 2, 3, 4, 5};
Fixed-size array
int *p = malloc(n * sizeof(int));
Heap allocation
free(p);
Release heap memory
Structs
struct Point { int x; int y; };
Struct definition
struct Point p = {3, 4};
Struct literal
p.x
Access a field
struct Point *ptr = &p; ptr->x
Access a field through a pointer
typedef struct { int x; int y; } Point;
typedef to drop the struct keyword
Strings & I/O
char name[20] = "Ada";
Fixed-size string (null-terminated char array)
printf("%d %s %.2f\n", n, s, f);
Formatted output
scanf("%d", &n);
Formatted input (needs &)
strlen(s); strcpy(dst, src); strcmp(a, b);
Common string.h functions
Functions
int add(int a, int b) { return a + b; }
Function definition
void modify(int *p) { *p = 10; }
Pass by pointer to modify the caller's value
int (*fn)(int, int) = add;
Function pointer
Cookies & your privacy
We use essential cookies to keep this site working and secure.
With your permission, we'd also like to use Google Analytics cookies to understand site traffic.
We don't use advertising or social media tracking cookies, and we honor Global Privacy Control (GPC) browser signals.
Learn more in our Privacy Policy .
Your browser is sending a GPC signal, so analytics stays off even if you accept.
Accept
Reject