C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>
int main()
{
    char ch;
    if(ch = printf(""))
        printf("It matters\n");
    else
        printf("It doesn't matters\n");
    return 0;
}
It matters
It doesn't matters
matters
No output
Answer: Option
Explanation:

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

Step 1: if(ch = printf("")) here printf() does not print anything, so it returns '0'(zero).
Step 2: if(ch = 0) here variable ch has the value '0'(zero).
Step 3: if(0) Hence the if condition is not satisfied. So it prints the else statements.
Hence the output is "It doesn't matters".

Note: Compiler shows a warning "possibly incorrect assinment".

Discussion:
18 comments Page 1 of 2.

ALI said:   1 decade ago
printf always return no of character.

Do that you can understand better.

printf("%d",printf("%d",printf("Hi how are you"))).

Vasavi said:   4 years ago
Here it is like;
If(pf=( ) ) means 0,
So, ch=0
If(0) it means condition is false.
So, else is printed.

Vijay said:   6 years ago
When I use:

#include<stdio.h>
int main()
{
char ch;
if(ch == printf(""))
printf("It matters\n");
else
printf("It doesn't matters\n");
return 0;
}

Why does it print it matters?

Sabari Ganesh said:   6 years ago
if( ch = 0) // it's true
In case ch = 5
if (ch = 0) // this also true Statement because here we used single equal.

Siddesh said:   7 years ago
In printf (" "); there is a space so it takes space as a character and assigns value ch=1.

So, the output will be It matters.

Neelakandan said:   9 years ago
#include<stdio.h>
int main()
{
char ch;
if(ch = printf("Hai"))
printf("\nIt matters\n");
else
printf("It doesn't matters\n");
return 0;
}

It is executed and prints "Hai" follows "It matters", character datatype assigns only one character as well as if condition checking only for '==' how will works?.

Rohih said:   9 years ago
Hey I have a doubt here in printf (==) if it is there it works. So the above question doesn't works.

Mitali said:   1 decade ago
Here, printf() is a function that is used to evaluate the condition in if statement. We need to check whether printf() is printing anything.

If yes, then the If statement will evaluate to true. If it does not print anything then the if statement evaluate to false.

In this program printf() function doesn't print anything, so the condition is false and hence the result "It doesn't matters". Also the variable being char or integer doesn't matter.

Monika said:   1 decade ago
if(ch=printf(""))
printf("matters");
else
printf("it does not matters");

The above snippet will not execute in Turbo C, it will show error IF statement, its incorrect statement.

Error: "possibly in correct statement".

Rahul said:   1 decade ago
printf("") returns 0 to any variable then
ch=0
if (ch)means if(ch!=0) which is not true anytime so else expression will be printed


Post your comments here:

Your comments will be displayed after verification.