Order of execution of function calls within a line - undefined?

后端 未结 2 409
庸人自扰
庸人自扰 2021-01-15 14:30

p.77 K&R C says the output of the following program is undefined, but current version of Xcode gives no warning. Is it still undefined in current standard? What about in

2条回答
  •  粉色の甜心
    2021-01-15 14:52

    A compiler won't warn you about all undefined programs. In the following line:

    printf("%d" , my_m() - my_m());
    

    the order of the calls my_m() is unspecified. The compiler might decide to call either the first one or the second one first. Since the function returns 42 on the first call and 57 on the second call the result is either 42 - 57 or 57 - 42.

    Edit: I missed the difference between undefined and unspecified, see Jerry Coffins answer for details about that.

提交回复
热议问题