0 votes 0 votes when i printing x it is giving 0 but when comparing x with 5 ,it executing else part ,means condition fail ,why ?? #include <stdio.h> int main(void) { int x; if(x=(printf("Hello")) != 5) printf("Hello"); else printf("World \n"); printf("%d \n",x); return 0; } https://ideone.com/NJSj2i Programming in C data-structures programming-in-c output + – Gurdeep Saini asked Jan 28, 2019 Gurdeep Saini 393 views answer comment Share Follow See all 3 Comments See all 3 3 Comments reply OneZero commented Jan 28, 2019 reply Follow Share Your forgot to put parenthesis. what is happening in the above code : printf is returning 5 which is being compared with 5 before storing the value in variable x because the precedency of != is more than =. after comparing 5 != 5, which is false, it will ruturn 0. This 0 is stored in the variable x. int main() { int x = 10; if((x=(printf("Hello"))) != 5) printf("Hello"); else printf("World \n"); printf("%d \n",x); return 0; } 2 votes 2 votes Gurdeep Saini commented Jan 28, 2019 i edited by Gurdeep Saini Jan 28, 2019 reply Follow Share thanks @OneZero you really pointed out the mistake edited comment 0 votes 0 votes OneZero commented Jan 28, 2019 reply Follow Share @Gurdeep Saini Its OneZero :P 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes yes as printf returns number of characters it print so here printf("Hello") will return 5 and the condition 5!=5 become false due to which x will get 0.Change "HELLO" with "HY" 2!=5 true hence x=1 adeemajain answered Jan 28, 2019 adeemajain comment Share Follow See all 0 reply Please log in or register to add a comment.