Interview Questions

I once used a compiler that wouldn't work unless NULL was used.

C Interview Questions and Answers


(Continued from previous question...)

I once used a compiler that wouldn't work unless NULL was used.

Unless the code being compiled was nonportable, that compiler was probably broken.
Perhaps the code used something like this nonportable version of an example
execl("/bin/sh", "sh", "-c", "date", NULL); /* WRONG */
Under a compiler which defines NULL to ((void *)0) , this code will happen to work. However, if pointers and integers have different sizes or representations, the (equally incorrect) code
execl("/bin/sh", "sh", "-c", "date", 0); /* WRONG */
may not work.
Correct, portable code uses an explicit cast:
execl("/bin/sh", "sh", "-c", "date", (char *)NULL);
With the cast, the code works correctly no matter what the machine's integer and pointer representations are, and no matter which form of null pointer constant the compiler has chosen as the definition of NULL.

(Continued on next question...)

Other Interview Questions