Firstly your
printf("%s", 0);
leads to undefined (UB) behavior. %s in printf requires a char * pointer as an argument. You pass 0 , which corresponds to int . This in itself already violates your code, as
printf("%s", 42);
will be. For this particular UB, the fact that 0 is zero does not matter.
Secondly, if you really want to try passing the null-ponter format specifier to %s , you should do something like
printf("%s", (char *) 0);
Of course, this also leads to undefined behavior, since %s requires a pointer to a valid string as an argument, and (char *) 0 not a valid string pointer. But some implementations prefer to handle such situations gracefully and simply print (null) .
In your particular case, you were just lucky: printf("%s", 0) "worked" just like printf("%s", (char *) 0) , and your implementation saved the day by outputting (null) .
source share