Several people have given you the right answer; here's an explanation of why your attempts didn't work.
Why A[0] && A[1] && A[2] && A[3] == A[4]
didn't work:
The expression A && B
will evaluate to 0 if either A
or B
are zero, or to 1 if both A
and B
are non-zero. The &&
operator is left-associative, meaning expressions like A && B && C
are parsed as (A && B) && C
; IOW, C
is AND-ed with the result of A && B
(0 or 1). Thus, A[0] && A[1] && A[2] && A[3] == A[4]
will only evaluate to true if all of A[0]
through A[3]
are non-zero and A[4]
== 1, or any one of A[0]
through A[3]
are 0 and A[4]
== 0. Also note that the &&
operator won't evaluate the right-hand operand if the left-hand operand is 0.
Why (A[0]),(A[1]),(A[2]),(A[3]) == (A[4])
didn't work:
The comma operator evaluates each expression in sequence, and the result is the value of the last expression, so only the value of A[3]
is being compared to A[4]
. Each of A[0]
through A[2]
is evaluated, but the result is discarded.
Why (A[0]) == (A[1]) == (A[2]) == (A[3]) == (A[4])
didn't work:
Similar to the &&
operator, the result of A == B
will be 0 or 1. Also similar to the above, the ==
operator is left-associative, so A == B == C
is parsed as (A == B) == C
, so C
is being compared to the result of A == B
, which will be either 0 or 1. So the above will only evaluate to true if A[0]
and A[1]
are the same, and A[2]
through A[4]
all equal 1.
Therefore, if you want to check that multiple expressions are all equivalent, you must write something like
A == B && B == C && C == D