Look at the first argument to printf() .
"%d" + 1
This indicates the same as ptr in the following code.
char *ptr = "%d"; ptr = ptr + 1;
So what does it mean to increment a pointer? So we move the sizeof(*ptr) * 1 bytes sizeof(*ptr) * 1 forward.
So, in memory we have:
%d\0 ^^ || |This is where ("%d" + 1) points to. This is where ("%d") points to.
So your code is more or less functionally equivalent to executing:
short int a = 5; printf("d", a);
What will be evaluated, then ignore the optional function argument and print d .
One more thing:. You are very close to undefined behavior in this code. printf("%d", a) uses the wrong format string. The correct format string for short int is "%hd" .
You can find the full format string table here .
Bill lynch
source share