简体   繁体   English

C 中的序列点和 I/O 格式说明符

[英]Sequence points and I/O format Specifiers in C

The C standard (Annex C) states that there is a sequence point C标准(附件C)规定有一个序列点

After the actions associated with each formatted input/output function conversion specifier.在与每个格式化输入/输出函数转换说明符关联的操作之后。

Given that, why am I getting an unsequenced modification and access to i (clang) warning for the below?鉴于此,为什么我会收到未排序的unsequenced modification and access to i (clang) 警告?

int i = 0;
printf("%d, %d\n", i, ++i);

Based on the standard, there is a sequence point after the first and the second %d .根据标准,在第一个和第二个%d之后有一个序列点。 If so I should be getting a 0 1 ?如果是这样,我应该得到0 1吗? But then there is no ordering guarantee in the evaluation of function arguments and I could be getting 1 1 instead?但是在函数参数的评估中没有顺序保证,我可以得到1 1吗?

So what does the text of the standard I quoted really mean?那么我引用的标准文本到底是什么意思呢?

Given that, why am I getting an unsequenced modification and access to i (clang) warning for the below?鉴于此,为什么我会收到未排序的修改并访问以下 i (clang) 警告?

 int i = 0; printf("%d, %d\n", i, ++i);

Because the problem happens when evaluating the function's argument list, before the function is actually called.因为问题发生在评估函数的参数列表时,在实际调用函数之前。 The evaluations of multiple arguments to the same function call are not sequenced with respect to each other, and you both read and modify i via separate, unsequenced arguments.对同一个函数调用的多个参数的求值没有相互排序,并且你们都通过单独的、未排序的参数读取和修改i

The provision you cited is not relevant to this issue.你引用的条款与这个问题无关。 It describes sequence points between the I/O operations performed by the function when it executes.它描述了函数在执行时执行的 I/O 操作之间的顺序点。 Because function arguments are always passed by value, and because there is a sequence point between evaluating the arguments and executing the function body, I don't see any practical relevance of that provision to the printf -family functions.因为函数参数总是按值传递,并且因为在评估参数和执行函数体之间存在顺序点,所以我看不到该规定与printf系列函数有任何实际相关性。

For scanf & friends, however, the provision helps ensure that然而,对于scanf & friends,该条款有助于确保

int i;
scanf("%d, %d", &i, &i);

has well-defined behavior, because it specifies that the two resulting writes to i are sequenced with respect to each other.具有明确定义的行为,因为它指定对i的两个结果写入相对于彼此排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM