Skip to content

Instantly share code, notes, and snippets.

@leocb
Last active December 4, 2019 14:57
Show Gist options
  • Save leocb/5af808bd3140efecee04f1869ea1a4de to your computer and use it in GitHub Desktop.
Save leocb/5af808bd3140efecee04f1869ea1a4de to your computer and use it in GitHub Desktop.
C Cheatsheets

C pointers Cheatsheet

Increment / Decrement

Pointer action Memory Address Memory Contents
p Yes, it's an address No, this returns the address
*p No, this dereferenced the pointer Yes, this returns the value at that address
*p++ Incremented after value is read Unchanged
*(p++) Incremented after value is read Unchanged
(*p)++ Unchanged Incremented after it's used
*++p Incremented before value is read Unchanged
*(++p) Incremented before value is read Unchanged
++*p Unchanged Incremented before it's used
++(*p) Unchanged Incremented before it's used
p*++ Not a pointer Not a pointer
p++* Not a pointer Not a pointer

Array access

Array Notation Pointer Equivalent
array[0] *a
array[1] *(a+1)
array[x] *(a+x)

C printf Cheatsheet

%[flags][width][.precision][length]specifier

Flags

Symbol Description
0 Left-pads numbers with 0 instead of (spaces), where padding is specified ([see width](## Width)).
- Left-justify within the given field width; Right justification is the default ([see width](## Width)).
+ Forces numbers to be preceded by a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign
(space) If a number will not be preceeded by a sign, a blank space is inserted before the number.
# Specifiers o x X: The value is preceded with 0, 0x or 0X respectively for values different than zero.
Specifiers e E f: Forces the written output to contain a decimal point even if no digits would follow; By default, if no digits follow, no decimal point is written.
Specifiers g G: The result is the same as with e and E but trailing zeros are not removed.

Width

Symbol Description
(number) Minimum number of characters to be printed - If the value is shorter than this number then it's padded with blank spaces. This does NOT truncate the value if it's longer.
* Same as above, but instead of defining it as a fixed value in the format string, it's defined by an additional integer argument preceding the argument that will be formatted.

.precision

Symbol Description
.number Specifiers d i o u x X: Minimum number of digits to be written. If the value is shorter than this number then it's padded with zeroes (0). This does NOT truncate the value if it's longer. A precision of 0 means that no character is written for the value 0.
Specifiers e E f: Maximum number of digits to be printed after the decimal point.
Specifiers g G: Maximum number of significant digits to be printed.
Specifier s: Maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
Specifier c: Ignored.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
.* Same as above, but instead of defining it as a fixed value in the format string, it's defined by an additional integer argument preceding the argument that will be formatted.

Length

Symbol Description
h Specifiers d i o u x X: The argument is interpreted as a short int or unsigned short int.
l Specifiers d i o u x X: The argument is interpreted as a long int or unsigned long int.
Specifiers c s: The argument is interpreted as a wide character or wide character string
L Specifiers e E f g G: The argument is interpreted as a long double.

Specifier

Symbol Description
c Character
d or i Signed decimal integer
e Scientific notation using the 'e' character
E Scientific notation using the 'E' character
f Single or Double precision floating point
g Uses the shorter of %e or %f
G Uses the shorter of %E or %f
n Nothing printed
o Signed octal
p Pointer address
s Array of characters (must be null terminated or have .precision defined)
u Unsigned decimal integer
x Hexadecimal representation of the value
X Hexadecimal representation of the value (capital letters)
% prints the '%' Character
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment