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;
}
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.
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.
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.
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?.
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?.
Ganesh -(Gani) said:
1 decade ago
Mr. Sushant how can a if statement check the condition without == symbol.According to u ...U said that we are simply executing the program..But it is wrng bcoz if the 'if' statement comaparison is wrong then it goes to the 'else' statement, but here there is no comparison takes place. So how can u tell the out put is the statement in the else statement.....?????? ..Ganesh
Santosh Singh said:
1 decade ago
if(ch = printf ("")) , here printf("") will give 0 output and then 0 is assigned to variable ch {ch=0}, but we can't assign int value to char variable directly, for this we need explicit typecasting. So this expression ch=0; is false So condition inside if become false and thus the result.
Shiva said:
1 decade ago
Hai @all.
we can write == in the if condition. Bhaskar is asking what if we write == in that place.
Then it compares 0 with value in ch, since ch is a garbage value, 99.999999% times we will get else output. In condition ch is zero randomly we will get if statement executed. :)
we can write == in the if condition. Bhaskar is asking what if we write == in that place.
Then it compares 0 with value in ch, since ch is a garbage value, 99.999999% times we will get else output. In condition ch is zero randomly we will get if statement executed. :)
Sushant said:
1 decade ago
@Bhaskar,
Yes you do comparisons by "==", however we are not making a comparison over here. We are simple executing a statement in the if() statement and the output decides the condition to be true ie the result is 0(means false) or the result is >1 (means true)
Yes you do comparisons by "==", however we are not making a comparison over here. We are simple executing a statement in the if() statement and the output decides the condition to be true ie the result is 0(means false) or the result is >1 (means true)
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".
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".
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?
#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?
Amita said:
1 decade ago
Hello. Here in the if condition ch=0, means we are assigning a value to ch, so it will return 1 and should execute if condition. Please do clear my doubt if I am wrong.
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"))).
Do that you can understand better.
printf("%d",printf("%d",printf("Hi how are you"))).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers