What is the output of this C code?
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
Answer: A
Correct answer: nullp nullq
Both pointers are null - NULL and a literal zero in a pointer context mean the same thing - and a null pointer used in a condition tests false. So both if statements take their else branch, and the output is nullp nullq. The result does not depend on how NULL is defined, since the comparison is against the null pointer value rather than against the bit pattern.
Please log in or register to report this question.