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.
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.
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?
#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.
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.
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?.
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.
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".
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".
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"))).
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers