Nat! bio photo

Nat!

Senior Mull.

Twitter RSS

Github

printf and numbered arguments: inherently impossible to implement, yet works

Kempe:~ nat$ cat foo.c
#include <stdio.h>

main()
{
   printf( "%4$*3$.*2$s\n", 1LL, 5, 3, "hahaha", 2LL);
}
Kempe:~ nat$ cc -o foo foo.c
foo.c: In function ‘main’:
foo.c:5: warning: format argument 1 unused before used argument 4 in $-style format
Kempe:~ nat$ ./foo
hahah

There is no way, you can write printf. That's because you can't determine the size of the first parameter, which you need to access the parameters coming up on the stack properly.

Yet, printf manages to do just fine on MacOSX and Linux ?Reason being, that I compiled this for 64bits and I am still stuck in 32bit mentality. What a pointless weblog entry ;)