function parameter evaluation order

后端 未结 5 1878
攒了一身酷
攒了一身酷 2020-11-22 07:37

In C and C++, is there a fixed order for evaluation of parameter to the function? I mean, what do the standards say? Is it left-to-right or right-to-left<

5条回答
  •  醉话见心
    2020-11-22 07:55

    Just only to speak for C language, the order of evaluation inside the function parameters depend on compiler. from The C Programming Language of Brian Kernighan and Dennis Ritchie;

    Similarly, the order in which function arguments are evaluated is not specified, so the statement

    printf("%d %d\n", ++n, power(2, n)); /*WRONG */

    can produce different results with different compilers, depending on whether n is incremented before power is called. The solution, of course, is to write

    ++n;

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

提交回复
热议问题