well the o/p of the following fragment: if(a=0) printf("a is zero"); else printf("a is non zero");
i don't understand what do you want?
you mean the problem? You're assigning 0 to a.
Assuming a is declared, the conditional will always evaluate to 0 (or false), since the value of the expression within the if statement will always evaluate to 0, and therefore the statement "printf("a is non zero")" will be executes.
to catch these sorts of errors, you could try placing the numeric literal on the left-hand side of the expression: if (0 = a) // will give you an error something like lhs must be l-value anyways, compilers like gcc (with some warning settings turned on) will warn you whenever you have assignments within conditionals anyway.
oh, and I just figured out that when you mentioned 'o/p' in your question, you meant output :-P
One of the biggest butt biters in C and C derived languages is that "=" is an assignment operator, "==" is a comparison operator. I hate when I type it wrong!
yes o/p means output.sry for that. then the output of the above program is "a is non zero". I assume that output of the above one is "a is zero" since a has been successfully assigned and hence returns 1.so the output must be "a is zero" .I couldn't trace the reason.
have you tried placing == instead of =? and what language is it?
Join our real-time social learning platform and learn together with your friends!