该程序是否有任何序列点问题?

发布于 2024-09-16 16:17:23 字数 171 浏览 1 评论 0原文

#include<stdio.h>
 int main()
 {  
       int i=7,j;
       j=(i++,++i,j*i); 
       return 0;
}

j=(i++,++i,j*i);这个定义明确吗?让我澄清一下我的疑问。

#include<stdio.h>
 int main()
 {  
       int i=7,j;
       j=(i++,++i,j*i); 
       return 0;
}

j=(i++,++i,j*i);Is this well defined ? Let me clear my doubt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-09-23 16:17:23

这个表达式没问题,因为 逗号运算符 是一个 序列点

(i++, ++i, j*i)

但是,不要将其与以下内容混淆,其中逗号不充当序列点:

somefunction(i++, ++i, j*i)

j = i++ * ++i 怎么样

乘法运算符不是序列点。


请原谅我劫持您的答案

来自 ISO 9899:1999(C 标准)的 §3.4:

3.4行为

外表或动作

3.4.1 实现定义的行为

未指定的行为,其中每个实现都记录了如何做出选择

示例 实现定义行为的一个示例是高阶位的传播
当有符号整数右移时。

3.4.2 区域特定行为

行为取决于当地的国籍、文化和语言习俗
实施文件

示例 区域设置特定行为的一个示例是 islower 函数是否返回 true
26 个小写拉丁字母以外的字符。

3.4.3 未定义行为

使用不可移植或错误的程序构造或错误数据时的行为,
本国际标准没有对此提出要求

注意可能的未定义行为包括完全忽略情况和不可预测的情况
结果,在翻译或程序执行过程中以记录的方式表现
环境(有或没有发出诊断消息),终止翻译或
执行(发出诊断消息)。

示例 未定义行为的一个示例是整数溢出的行为。

3.4.4 未指定行为

本国际标准提供两种或多种可能性的行为,并且
在任何情况下都没有对选择的内容施加任何进一步的要求

示例 未指定行为的一个示例是函数参数的顺序
已评估。

This expression is OK because the comma operator is a sequence point:

(i++, ++i, j*i)

However do not confuse it with the following where the comma is not acting as a sequence point:

somefunction(i++, ++i, j*i)

What about j = i++ * ++i

The multiplication operator is not a sequence point.


(Excuse me hijacking your answer)

From §3.4 of ISO 9899:1999 (C Standard):

3.4 behavior

external appearance or action

3.4.1 implementation-defined behavior

unspecified behavior where each implementation documents how the choice is made

EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit
when a signed integer is shifted right.

3.4.2 locale-specific behavior

behavior that depends on local conventions of nationality, culture, and language that each
implementation documents

EXAMPLE An example of locale-specific behavior is whether the islower function returns true for
characters other than the 26 lowercase Latin letters.

3.4.3 undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirements

NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable
results, to behaving during translation or program execution in a documented manner characteristic of the
environment (with or without the issuance of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message).

EXAMPLE An example of undefined behavior is the behavior on integer overflow.

3.4.4 unspecified behavior

behavior where this International Standard provides two or more possibilities and
imposes no further requirements on which is chosen in any instance

EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are
evaluated.

埋葬我深情 2024-09-23 16:17:23

是的,它定义得很好。
序列点

C 中的逗号运算符

Yes, It's well defined.
sequence point

comma operator in C

我不吻晚风 2024-09-23 16:17:23

在您的代码中,“,”将作为序列点。

所以在这个

j=(i++,++i,j*i);

表达式中将从左到右工作。
所以首先 i++ 然后 ++i 然后 j*i

最后 j*i 将存储在 j 中;

但最后你的结果会很优雅,因为“ j ”没有预定义的数据
因此未定义的值将存储在 j 中。

如果您不使用“ () ”,

您的代码将作为单个语句工作,例如

j=i++;
++i;
j*i;

In your code " ," will be work as sequence point.

so in this

j=(i++,++i,j*i);

expression would be work from left to right.
so at first i++ then ++i and then j*i

at the last j*i would be stored in j;

but lastly your result would be elegant because " j " have no predefined data
so undefined value would be stored in j.

if you don't use " () "

your code would be work as single statement such as

j=i++;
++i;
j*i;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文