int x = 15 ;
printf ( "\n%d \t %d \t %d", x != 15, x = 20, x < 30 ) ;
The output of the code is 1 20 1 but I assume it should be 0 20 1 since 15 == 15…
I am facing a problem with the “x != 15” part
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In my experience, most lists of arguments are processed from the right to the left under most C / C++ compilers, even though the specification makes no statement about the required order of evaluation.
With such an understanding of how many compilers work, your list of arguments would be evaluated like so
evaluates (possibly) in the order of
If this evaluation order holds for your compiler, then rearranging the arguments like so
should yeild
because the comparison
x != 15will occur beforexis reassigned to 20.The lesson of this exercise is to generally avoid assignments in list constructs (things that look like “a, b, c, d”) or at least not to read assigned variables within the same list construct, as you cannot be assured of right to left or left to right evaluation (it’s compiler dependent).