I find myself often having trouble picking the right format string specifiers (or directives), so this is a cheat sheet. Maybe this can help others too.
These lists are not exhaustive (and not guarenteed to be platform agnostic), but will be updated as I encounter new types and directives.
- char
%c
(or%hhi
for numerical)- signed char
- unsigned char
%c
(or%hhu
for numerical) - short
%hi
- short int
- signed short
- signed short int
- unsigned short
%hu
- unsigned short int
- int
%d
(or%i
)- int
- signed
- signed int
- unsigned
%u
- unsigned int
- long
%li
- long int
- signed long
- signed long int
- unsigned long
%lu
- unsigned long int
- long long
%lli
- long long int
- signed long long
- signed long long int
- unsigned long long
%llu
- unsigned long long int
- float
%f
- double
%lf
- long double
%Lf
- void *
%p
- off_t
- cast to
long int
and use%li
- cast to
- pid_t
- casst to
long int
and use%li
- casst to
- mode_t
- print in octal
%o
- print in octal
- size_t
%zu
- ssize_t
%zi
If variable is of Type, use printk format specifier:
------------------------------------------------------------
char %d or %x
unsigned char %u or %x
short int %d or %x
unsigned short int %u or %x
int %d or %x
unsigned int %u or %x
long %ld or %lx
unsigned long %lu or %lx
long long %lld or %llx
unsigned long long %llu or %llx
size_t %zu or %zx
ssize_t %zd or %zx
s8 %d or %x
u8 %u or %x
s16 %d or %x
u16 %u or %x
s32 %d or %x
u32 %u or %x
s64 %lld or %llx
u64 %llu or %llx