The C Programming Language by K & R states that C, like most languages, does not specify the order in which operands of an operator are evaluated. (The exceptions are &&,||,?: and ','). According to the book, the result of the statement :

printf("%d %d\n",++x,power(2,x));

depends on the compiler. But it itself says in the previous line,that I have quoted that it does not predict the order of evaluation,except ',' and others. So don't you think book is contradicting itself?

  • I assume KKR was meant to be K & R? – jk. Jun 5 '13 at 9:24

The first statement (the exceptions) refers to the comma operator, this is (usually) different to commas in function calls.

The comma operator is used rarely, but can be used to do things like this:

int i,j; /* not comma operator */
for(i=0,j=0; i<10 && j<10; ++i, ++j) /* comma operators */
    doProcWith2Args(i,j); /*not comma operator*/

evaluation of operands of the comma operator is always left to right (it forms a sequence in standardese). Normal function parameter commas do not promise anything about their order of evaluation (and in general a program that changed depending on order of evaluation will be said to exhibit 'undefined behaviour').

I don't have my copy of K&R here but I suspect if you re-read the part with the knowledge of the comma operator it may make more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.