Image

(image: pixabay.com)

Correct C/C++ format string for printf()/scanf()

Using the correct format string for printf(), scanf(), etc. is not trivial. Furthermore, not all compiler warn when a wrong specifier is being used, which may lead to a crash.

... Bild by Markus Fleschutz ยท ๐Ÿ“… June 06, 2019

DATATYPE      โ†’ SPECIFIER TO USE
================================
int           โ†’ %d for decimal

unsigned int  โ†’ %u for decimal, %x for hex and %o for octal

long          โ†’ %ld for decimal

unsigned long โ†’ %lu for decimal, %lx for hex and %lo for octal

size_t        โ†’ %zu for decimal or %zx for hex

float         โ†’ %f

double        โ†’ %lf

char *        โ†’ %s for a C string

void *        โ†’ %p to print the pointer itself