C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 29)
29.
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;
}
The string is not empty
The string is empty
No output
0
Answer: Option
Explanation:

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.

In the else part it prints "The string is empty".

Discussion:
5 comments Page 1 of 1.

John Doe said:   1 decade ago
I believe the explanation is not the best one.
printf returns the number of bytes printed not including the trailing '\0' used to end output to strings.Therefore the return will be 0 and the condition is false.

Ask said:   10 years ago
I am not agree with the @John. Output would be A.

Santhosh said:   9 years ago
Output will be A because it will return 1.

NADS said:   8 years ago
Answer is A because it is in double quotes not single.

Akshay said:   7 years ago
The Answer is A.

Post your comments here:

Your comments will be displayed after verification.