Quick reference for printf, operators, types, and more. Bookmark this page!
| Format | Type | Description | Example | Output |
|---|---|---|---|---|
| %d | int | Decimal integer | printf("%d", 42) | 42 |
| %ld | long | Long integer | printf("%ld", 100000L) | 100000 |
| %u | unsigned | Unsigned integer | printf("%u", 42) | 42 |
| %f | double | Float (6 decimal places) | printf("%f", 3.14) | 3.140000 |
| %.2f | double | Float (2 decimal places) | printf("%.2f", 3.14) | 3.14 |
| %e | double | Scientific notation | printf("%e", 0.001) | 1.000000e-03 |
| %c | char | Single character | printf("%c", 'A') | A |
| %s | char* | String | printf("%s", "Hi") | Hi |
| %p | void* | Pointer address (hex) | printf("%p", &x) | 0x7fff... |
| %x / %X | unsigned | Hexadecimal | printf("%x", 255) | ff / FF |
| %o | unsigned | Octal | printf("%o", 8) | 10 |
| %5d | int | Right-aligned, width 5 | printf("%5d", 42) | 42 |
| %-5d | int | Left-aligned, width 5 | printf("%-5d", 42) | 42 |
| %05d | int | Zero-padded, width 5 | printf("%05d", 42) | 00042 |
| %% | - | Literal % character | printf("100%%") | 100% |
| Format | Type | Note |
|---|---|---|
| %d | int * | scanf("%d", &x) — don't forget & |
| %f | float * | For float (NOT double!) |
| %lf | double * | Use %lf for double (printf uses %f for both) |
| %c | char * | Reads one character (including whitespace) |
| %s | char[] | Reads string (until whitespace). No & needed |
%lf for double, but printf uses %f. This is the #1 scanf bug.| Sequence | Meaning |
|---|---|
| \n | Newline |
| \t | Tab |
| \\ | Backslash |
| \" | Double quote |
| \' | Single quote |
| \0 | Null character (string terminator) |
| Type | Size (typical) | Range | printf | scanf |
|---|---|---|---|---|
| char | 1 byte | -128 to 127 | %c | %c |
| short | 2 bytes | -32768 to 32767 | %hd | %hd |
| int | 4 bytes | ~±2.1 billion | %d | %d |
| long | 4 or 8 bytes | Platform-dependent | %ld | %ld |
| long long | 8 bytes | ~±9.2×10¹⁸ | %lld | %lld |
| float | 4 bytes | ~7 digits precision | %f | %f |
| double | 8 bytes | ~15 digits precision | %f | %lf |
| # | Operators | Description | Associativity |
|---|---|---|---|
| 1 | () [] -> . | Parentheses, array, member access | Left→Right |
| 2 | ! ~ ++ -- + - * & (cast) sizeof | Unary operators | Right→Left |
| 3 | * / % | Multiplication, division, modulo | Left→Right |
| 4 | + - | Addition, subtraction | Left→Right |
| 5 | << >> | Bitwise shift | Left→Right |
| 6 | < <= > >= | Relational | Left→Right |
| 7 | == != | Equality | Left→Right |
| 8 | & | Bitwise AND | Left→Right |
| 9 | ^ | Bitwise XOR | Left→Right |
| 10 | | | Bitwise OR | Left→Right |
| 11 | && | Logical AND | Left→Right |
| 12 | || | Logical OR | Left→Right |
| 13 | ?: | Ternary | Right→Left |
| 14 | = += -= *= /= ... | Assignment | Right→Left |
| 15 | , | Comma | Left→Right |
| Header | Functions | Purpose |
|---|---|---|
| stdio.h | printf, scanf, fprintf, fscanf, fgets | Input/Output |
| stdlib.h | malloc, calloc, free, realloc, atoi, rand | Memory, conversion, random |
| string.h | strlen, strcpy, strcat, strcmp, memcpy | String operations |
| math.h | sqrt, pow, abs, sin, cos, log, fabs | Math functions |
| ctype.h | isalpha, isdigit, toupper, tolower | Character classification |
| time.h | time, clock | Time functions |
| limits.h | INT_MAX, INT_MIN, CHAR_MAX | Type limits |