Can you tell me the reason behind the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you tell me the reason behind the output?

#include<stdio.h> int main() { int c=0; if(c=0) printf("%d",c=0); else printf("%d",c=1); printf("%d\n",c); }

12th May 2021, 7:19 AM
Hridoy Banik
3 Answers
+ 3
So I guess the output is 11? You assigned 0 to c, so 'if(c=0)' is false , cuz 0 returns false. You are not checking if c is equal to zero, but you are trying to assign 0 to c. Try c==0 to compare. So when your if does not execute, you jump to 'else' printing this c=1 (is output 1 or 0?). Then you go out of 'else' and print 1, as you assigned 1 to c in previous printf
12th May 2021, 8:02 AM
Michal Doruch
+ 5
Assignment operator(=) operates by assigning the value on left hand side to the variable on right hand side and returns the value it just assigned. In your case "if(c = 0)" will evaluate to "if(0)" which is false so the control goes in else part where "printf("%d",c=1)" tells your system to assign value 1 to the variable "c" and print it on the screen. Outside it, the second printf() is just printing the current value in variable "c" which is 1. Thus the output would be 11
12th May 2021, 7:57 AM
Arsenic
Arsenic - avatar
+ 1
Thanks Arsenic
12th May 2021, 8:00 AM
Hridoy Banik