+14

Why output of this code is 6??

https://code.sololearn.com/cmeNSzMFE5S6/?ref=app

c

5/22/2019 2:43:30 PM

Yamin Mansuri

8 Answers

New Answer

+14

If you don't use curly brackets, it'll only execute one line of code; it's not like Python int i = 3; if (!i) /*i to boolean is true because its not 0, so it is false*/ i++; /*this wont execute*/ i++;/*this will*/ if (i == 3) /*this is false because i equals 4*/ i += 2; /*this wont execute*/ i += 2; /*this will*/ 3 + 1 + 2 = 6

+11

Airree Thank you very much 😁😁

+5

Because it lacks braces on the ifs.

+5

Without {} brackets if control only one line.... your both condition is false... If(not true)=false i=3; i++; //then i become 4; Second condition false i+=2; //i become 6; Finally your answer is 6 ;

+4

it's because you haven't given curly braces. I tried check this out👍 #include <stdio.h> int main() { int i=3; if(!i){ i++; i++; printf("%d",i); } if(i==3){ i+=2; i+=2; printf("%d",i); } return 0; }

+4

Check out this code in Python - the output is 6. i=3 if(i==False): i+=1 if(i==3): i+=2 i+=1 print("The output is - "+ str(i))

+2

write code you miss to add curly braces check it out #include <stdio.h> int main() { int i=3; if(!i){ i++; i++; printf("%d",i); } if(i==3){ i+=2; i+=2; printf("%d",i); } return 0; }

-2

Please check it once again .... It's showing 7