Machine-translated by Google. Questions are authored in English.
Top

Discussion

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");
}

 

  • A.nullp  nullq
  • B.Depends on the compiler
  • C.x nullq where x can be p or nullp depending on the value of NULL
  • D.p   q

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.