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

Discussion

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is empty\n");
    else
        printf("The string is not empty\n");
    return 0;
}

 

  • A.The string is empty
  • B.The string is not empty
  • C.No output

Answer: B

Correct answer: The string is not empty

a holds a single null character, so printf("%s", a) writes nothing and returns 0 - the count of characters printed. Zero is false, so the else branch runs. The message is misleading, which is the point of the question: the condition tests how much was printed, not whether the string was empty.

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

Post your comment

Please log in or register to post a comment.