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 not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}

 

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

Answer: B

Correct answer: The string is 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 and the message happens to be correct. (Compare the variant where the two branches are the other way round, and the same logic prints the wrong message.)

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

Post your comment

Please log in or register to post a comment.