+ 1

Why this program prints both true and false

#include <stdio.h> int main() { int a=1; if (a--) printf("true\n"); if (++a) printf("false"); return 0; }

26th Feb 2019, 8:10 AM
Shaik Abeedh
Shaik Abeedh - avatar
1 Odpowiedź
+ 11
a = 1; if(a--) // here <a> is evaluated before decremented (<a> is still 1, thus considered true) printf("true\n"); // here <a> has been decremented and becomes zero if(++a) // here <a> is incremented and has become 1 before it is evaluated (yields true) printf("false"); Ask away if it wasn't clear Hth, cmiiw
26th Feb 2019, 8:31 AM
Ipang