C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 15)
15.
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;
}
The string is empty
The string is not 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 not empty".

Discussion:
6 comments Page 1 of 1.

Subrat mishra said:   1 decade ago
Here a[] is empty and in if statement we are printing a[], hence nothing will be printed. Only the else part was executed because printf inside if returns the no. Of character it prints. And here no of character it is printing is zero.

Abhimanyu Nagrath said:   1 decade ago
@Ravi.

printf() is not printing any thing because the string mentioned is empty and in if it will also return the number of character printed which is 0. So it will proceed in else block instead of if.

Ravi Ranjan Kumar said:   1 decade ago
Please tell me why the printf() within the "if(printf("%s",str)" does not print anything in the above question.

Shaik said:   8 years ago
0 is right answer because there is no condition in if.
i.e. If(0).
There is no meaning in it.

Selvadinesh said:   1 decade ago
@Ravi.

Its because array is a group of consecutive elements terminated by "\0".

Ravi Mule said:   9 years ago
But I got 0 as answer.

Post your comments here:

Your comments will be displayed after verification.