Expert C Programming

(Jeff_L) #1

Most of these become more understandable if you sit down to consider them at length. The case
involving the comma occasionally causes conniption fits in programmers, though. For example, when
this line is executed:


i=1,2;


what value does i end up with? Well, we know that the value of a comma operator is the value of the
rightmost operand. But here, assignment has higher precedence, so you actually get:


(i=1), 2; / i gets the value 1 /


i gets the value 1; then the literal 2 is evaluated and thrown away. i ends up being one, not two.


In a posting on Usenet some years ago, Dennis Ritchie explained how some of these anomalies are
historical accidents.


Software Dogma

Free download pdf