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 2 of 2.
Bhaskar said:
1 decade ago
Hope, we all knew about in if condition comparison can be done with '==' operator not with single '=' operator. Then how could we get output.
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
ch=0
if (ch)means if(ch!=0) which is not true anytime so else expression will be printed
Priyanka said:
1 decade ago
What Bhaskar said is true because if you want to check the condition one should use '==' operator the program cannot be compiled.
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.
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.
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.
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.
Vaibhav said:
1 decade ago
printf always written length of string that is to be printed hence ch = 0
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers