-
true or false
Hello all
I'm a novice both in c programming and this forum
this is a true or false question :
-----------------------------------------------------
The following exrpession assigns 2 in x.
(3 > 4 ) && ( x=2)
-----------------------------------------------------
Thanks in advance
-
Think of how operator && behaves when its left hand operand is false. Does it go on to evaluate its right hand operand?
-
What laserlight is saying is . . .
If the left side of a && expression is evaluated to false, it does not evaluate the second, right side, as it would be useless. If the left side is false, then the result is always false.
-
ok guys i get your point
so what if i change it a bit :
( 4>3 ) && ( x=2 )
would that be true ?
Meaning , if the very next line of code is
printf("%d",x)
will i get a
2
on the screen ?
Thanks for all the help
-
Well, if you tried it . . .
Code:
#include <stdio.h>
int main(int argc, char *argv[]) {
int x = 0;
(4>3) && (x = 2);
printf("%i\n", x);
return 0;
}
. . you get . . .
So, then, yes, you are correct.