🇯🇵 日本語 | 🇺🇸 English
Advertisement

C Language Cheat Sheet

Quick reference for printf, operators, types, and more. Bookmark this page!

printf Format Specifiers

FormatTypeDescriptionExampleOutput
%dintDecimal integerprintf("%d", 42)42
%ldlongLong integerprintf("%ld", 100000L)100000
%uunsignedUnsigned integerprintf("%u", 42)42
%fdoubleFloat (6 decimal places)printf("%f", 3.14)3.140000
%.2fdoubleFloat (2 decimal places)printf("%.2f", 3.14)3.14
%edoubleScientific notationprintf("%e", 0.001)1.000000e-03
%ccharSingle characterprintf("%c", 'A')A
%schar*Stringprintf("%s", "Hi")Hi
%pvoid*Pointer address (hex)printf("%p", &x)0x7fff...
%x / %XunsignedHexadecimalprintf("%x", 255)ff / FF
%ounsignedOctalprintf("%o", 8)10
%5dintRight-aligned, width 5printf("%5d", 42)   42
%-5dintLeft-aligned, width 5printf("%-5d", 42)42   
%05dintZero-padded, width 5printf("%05d", 42)00042
%%-Literal % characterprintf("100%%")100%

scanf Format Specifiers

FormatTypeNote
%dint *scanf("%d", &x) — don't forget &
%ffloat *For float (NOT double!)
%lfdouble *Use %lf for double (printf uses %f for both)
%cchar *Reads one character (including whitespace)
%schar[]Reads string (until whitespace). No & needed
Key difference: scanf uses %lf for double, but printf uses %f. This is the #1 scanf bug.

Escape Sequences

SequenceMeaning
\nNewline
\tTab
\\Backslash
\"Double quote
\'Single quote
\0Null character (string terminator)

Data Types and Sizes

TypeSize (typical)Rangeprintfscanf
char1 byte-128 to 127%c%c
short2 bytes-32768 to 32767%hd%hd
int4 bytes~±2.1 billion%d%d
long4 or 8 bytesPlatform-dependent%ld%ld
long long8 bytes~±9.2×10¹⁸%lld%lld
float4 bytes~7 digits precision%f%f
double8 bytes~15 digits precision%f%lf
Advertisement

Operator Precedence (Highest to Lowest)

#OperatorsDescriptionAssociativity
1() [] -> .Parentheses, array, member accessLeft→Right
2! ~ ++ -- + - * & (cast) sizeofUnary operatorsRight→Left
3* / %Multiplication, division, moduloLeft→Right
4+ -Addition, subtractionLeft→Right
5<< >>Bitwise shiftLeft→Right
6< <= > >=RelationalLeft→Right
7== !=EqualityLeft→Right
8&Bitwise ANDLeft→Right
9^Bitwise XORLeft→Right
10|Bitwise ORLeft→Right
11&&Logical ANDLeft→Right
12||Logical ORLeft→Right
13?:TernaryRight→Left
14= += -= *= /= ...AssignmentRight→Left
15,CommaLeft→Right
When in doubt, use parentheses (). Don't memorize this table — just use explicit grouping for clarity.

Common Standard Library Functions

HeaderFunctionsPurpose
stdio.hprintf, scanf, fprintf, fscanf, fgetsInput/Output
stdlib.hmalloc, calloc, free, realloc, atoi, randMemory, conversion, random
string.hstrlen, strcpy, strcat, strcmp, memcpyString operations
math.hsqrt, pow, abs, sin, cos, log, fabsMath functions
ctype.hisalpha, isdigit, toupper, tolowerCharacter classification
time.htime, clockTime functions
limits.hINT_MAX, INT_MIN, CHAR_MAXType limits
Advertisement