What does this code mean in C, " int x = ~!printf; "?

225 Views Asked by At
int x = ~!printf;
printf("%d\t%x",x,x);

It gives : -1 ffff

Can anyone explain ?

1

There are 1 best solutions below

4
On BEST ANSWER

printf without arguments is the function pointer, worth a non-zero value (it's built-in so the pointer cannot be zero)

Now you apply logical negation (!) on this non-zero value: you get zero.

Now negate this zero bit-wise (using bit to bit negation ~), you get all 1s in the int bit range (which can vary depending on the compiler)

Printing it in decimal yields -1, and in hexadecimal yields a given number of fs, depending on the size of the integer (on my 32bit integer compiler, I get ffffffff)

(note that -specially the negation of the function pointer- cannot be part of some valid program, this is only for academic purposes)