Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

well the o/p of the following fragment: if(a=0) printf("a is zero"); else printf("a is non zero");

OpenStudy (anonymous):

i don't understand what do you want?

OpenStudy (anonymous):

you mean the problem? You're assigning 0 to a.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

oh, and I just figured out that when you mentioned 'o/p' in your question, you meant output :-P

OpenStudy (rsmith6559):

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!

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

have you tried placing == instead of =? and what language is it?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!