
August 7th, 2004, 09:18 PM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by davekw7x
Now here's one that has an unambiguously unequivocal "right" answer, since the C standard says that expressions connected by the "||" are evaluated left to right, and whenever a non-zero value is found for an term in the expression, no further evaluation takes place. Note that ++ has a higher precedence than ||, so the first thing that is evaluated is ++a. This sets a to a new value 1, and the value of the expression is the new value of a, and since this is non-zero, no further evaluation takes place past the ||.
Borland gives the same (correct) answer, as does Gnu gcc, and all of the other valid C compilers, including Microsoft Visual C++.
What's the answer?
compile and run this:
Code:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int r;
r=++a||++b||++c;
printf("a = %d, b = %d, c = %d, r = %d\n", a, b, c, r);
return 0;
}
If you don't get the following results, ask for your money back on your C compiler:
a = 1, b = 0, c = 0, r = 1
Dave
|
|