gpt4 book ai didi

c - 这是 printf 函数的未定义行为吗?

转载 作者:行者123 更新时间:2023-11-30 17:00:22 25 4
gpt4 key购买 nike

 int c=2; printf("%d %d %d",c,c<<=2,c>>=2);

这是未定义的行为吗?

 int c=2;
printf ("%d %d %d",c,c<<=2,c>>=2);

最佳答案

行为未定义。

[C99: 4/2]: If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’.

[C99: 5.1.2.3/2]: Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects,11) which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (A summary of the sequence points is given in annex C.)

[C99: C/1]: The following are the sequence points described in 5.1.2.3: - The call to a function, after the arguments have been evaluated (6.5.2.2). - The end of the first operand of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17). - The end of a full declarator: declarators (6.7.5); - The end of a full expression: an initializer (6.7.8); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4). - Immediately before a library function returns (7.1.4). - After the actions associated with each formatted input/output function conversion specifier (7.19.6, 7.24.2). - Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.20.5).

[C99: 6.2.2.2/10]: The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

而且,虽然我无法证明是否定的,但没有任何措辞可以对 varargs 情况进行异常(exception)处理。

关于c - 这是 printf 函数的未定义行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com