Menu

C Cheat Sheet

Last updated

Hello World & program structure

Every C program starts at main and returns an int.

ElementCode
Include a header#include <stdio.h>
Entry pointint main(void) { ... }
With command-line argsint main(int argc, char *argv[]) { ... }
Print a lineprintf("Hello, World!\n");
Return successreturn 0;
Single-line comment// comment
Block comment/* comment */

Data types

Sizes are platform-dependent; the minimums below are guaranteed by the standard.

TypeDescription
intInteger, at least 16 bits (usually 32)
unsigned intNon-negative integer
long / long longWider integers (at least 32 / 64 bits)
float / doubleSingle / double precision floating point
charSingle byte, also used for characters
_Bool (bool via <stdbool.h>)true or false
size_tUnsigned type for sizes and indices
voidNo type - used for functions and generic pointers

printf & scanf format specifiers

Placeholders that match the type of each argument.

SpecifierMatches
%d / %iSigned int
%uUnsigned int
%ld / %lldlong / long long
%fdouble (and float in printf)
%.2fdouble with 2 decimal places
%cSingle char
%sString (char *)
%pPointer address
%xUnsigned int in hexadecimal
%zusize_t

Operators

OperatorMeaning
+ - * / %Arithmetic (% is integer remainder)
== !=Equal / not equal
< > <= >=Comparison
&& || !Logical and / or / not
& | ^ ~ << >>Bitwise and, or, xor, not, shifts
++ --Increment / decrement
+= -= *= /=Compound assignment
cond ? a : bTernary conditional
sizeof(x)Size of a type or variable in bytes

Control flow

StatementSyntax
If / elseif (x > 0) { ... } else { ... }
Else ifelse if (x == 0) { ... }
Switchswitch (n) { case 1: ...; break; default: ...; }
While loopwhile (i < n) { ... }
Do-while loopdo { ... } while (i < n);
For loopfor (int i = 0; i < n; i++) { ... }
Break / continuebreak; exits a loop, continue; skips to next iteration
Goto (rare)goto label; ... label:

Functions

Declare before use, or add a prototype at the top of the file.

OperationSyntax
Define a functionint add(int a, int b) { return a + b; }
Prototype (declaration)int add(int a, int b);
No return valuevoid greet(void) { ... }
No parametersint rand_seed(void) { ... }
Pass by pointer (mutate)void inc(int *p) { (*p)++; }
Pass an arrayint sum(int arr[], int n) { ... }
Static (file-local)static int helper(void) { ... }

Pointers & memory

A pointer holds an address. Heap memory must be freed manually.

OperationSyntax
Declare a pointerint *p;
Address-ofp = &x;
Dereference (read/write)int y = *p; / *p = 10;
Null pointerint *p = NULL;
Allocate on the heapint *a = malloc(n * sizeof(int));
Allocate + zeroint *a = calloc(n, sizeof(int));
Resize an allocationa = realloc(a, newSize);
Free heap memoryfree(a);
Pointer arithmetic*(a + i) is the same as a[i]

Arrays & strings

Strings in C are char arrays terminated by \0.

OperationSyntax
Declare an arrayint nums[5];
Initialize an arrayint nums[] = {1, 2, 3};
Access an elementnums[0] = 10;
Array length (stack arrays)sizeof(nums) / sizeof(nums[0])
String literalchar s[] = "hello";
String lengthstrlen(s) (excludes \0)
Copy a stringstrcpy(dst, src);
Compare stringsstrcmp(a, b) == 0 means equal
Concatenate stringsstrcat(dst, src);

Structs & common stdlib

OperationSyntax
Define a structstruct Point { int x; int y; };
Declare & accessstruct Point p; p.x = 1;
Access via pointerp->x = 1; (same as (*p).x)
Type aliastypedef struct Point Point;
Read inputscanf("%d", &n);
Convert string to intint n = atoi("42");
Absolute value / powerabs(x) (int), pow(x, y) from <math.h>
Sort an arrayqsort(arr, n, sizeof(int), cmp);

The C syntax, format specifiers, and pointer patterns you reach for most, on one page. This C cheat sheet is a quick reference for writing C - the data types, printf/scanf specifiers, operators, control flow, and the pointer and memory operations that trip people up.

Everything here is standard C (C99/C11) and compiles with gcc or clang. Copy what you need, or try any snippet live in the C playground - no compiler to install.

C cheat sheet FAQ

Is this C cheat sheet free?
Yes. This C cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up a type, format specifier, or pointer operation.
What is the difference between the stack and the heap in C?
Stack memory holds local variables and is freed automatically when a function returns - fast, but limited in size. Heap memory is allocated explicitly with malloc or calloc, lives until you call free, and is used when you need memory that outlives the current function or whose size is known only at runtime. Forgetting to free heap memory causes a memory leak.
Why does scanf need an ampersand (&) but printf does not?
scanf writes the value back into your variable, so it needs the variable's address - that is what &x gives it. printf only reads the value, so you pass the value itself. The exception is strings: a char array already decays to a pointer, so you pass scanf("%s", name) without the &.
Can I practice C online?
Yes. Open the C playground to compile and run any snippet from this cheat sheet in your browser - no compiler to install. When you want structure, Coddy's free interactive C course takes you from variables and loops to pointers and memory management step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the most common building blocks (types, printf, control flow) down to advanced ones (pointers, memory, structs), so you can use the top sections on day one and grow into the rest.
Coddy programming languages illustration

Learn C with Coddy

GET STARTED