LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 5

NULL pointer vs empty string in conditions Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    const char *p = NULL;
    const char *q = "";
    printf("%d %d %d\n", p ? 1 : 0, q ? 1 : 0, q && *q);
    return 0;
}
  • A 0 1 0 Correct
  • B 0 0 0
  • C 0 1 1
  • D 1 1 0
Correct Answer: Option A

p is a null pointer (false). q is a valid pointer (true) to an empty string, whose first character is '\0' (false). "Not NULL" and "not empty" are two different checks.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links