有关逗号表达式的问题

liguomail_7 2003-10-20 02:22:28
我在看书的时候姓潭的写的遇到两个问题①关于赋值,前面他说不能写成a=b=1这样的,后面又说可以,到底能不能这样写啊?②就是逗号表达试的问题,一个逗号表达式x=a=3,6*a整个表达式的值是多少啊?我认为是18,大家能帮我吗?
...全文
749 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
maddevil 2003-10-27
  • 打赏
  • 举报
回复
to:wingfiring(别逗了)

我同意你关于副作用的说法 但逗号运算符应该不受副作用的影响?
以下这断话是The C++ Programming Language (The Third Edtion)的6.2.2里说的

The operators , (comma), && (logical and),and || (logical or)
guarantee that their left-hand operand is evaluated before their right-hand operand. For example,b=(a=2,a+1) assigns 3 to b.

你对这断话有什么看法


deeply 2003-10-27
  • 打赏
  • 举报
回复
测试环境:vc++6.0

int main(int argc, char* argv[])
{
int x,a;
x=a=3,6*a;
printf("x=%d\n",x);
printf("a=%d\n",a);
return 0;
}

测试结果
x=3
a=3
fierygnu 2003-10-27
  • 打赏
  • 举报
回复
to wingfiring(别逗了):
逗号表达式的运算顺序也是标准定义的。
haosjt 2003-10-27
  • 打赏
  • 举报
回复
结果一定是18
sakurar 2003-10-27
  • 打赏
  • 举报
回复
不同编译器会有不同
_码农一个_ 2003-10-27
  • 打赏
  • 举报
回复
哈,逗号运算符的问题。

我想题主应该是在学标准C时,遇到的吧?

如果是标准C,那么第二个问题的答案就一定是18了。
lilililll 2003-10-27
  • 打赏
  • 举报
回复
谭的书上说的是x的值,不是整个逗号表达式的值,
X=(A=3,6*A)是一个赋值表达式,将一个逗号表达式的值赋给X
lilililll 2003-10-27
  • 打赏
  • 举报
回复
如果是X=(A=3,6*A),则X的值是18;
若是X=A=3,6*A
此时X的值是3
ceran 2003-10-27
  • 打赏
  • 举报
回复
1.完全可以!老谭先说不行可能是想说明一些编程规范,谁知道呢?
2.答案18,应该没问题.
killme2008 2003-10-27
  • 打赏
  • 举报
回复
楼上,呵呵,抱歉,我是抄上面的试了一下,没注意看
答案确实是18
maddevil 2003-10-27
  • 打赏
  • 举报
回复
你这样是不对的
应该这样:
int main(int argc, char* argv[])
{
int x,a,k;
k=(x=a=3,6*a);
printf("k=%d\n",k);
return 0;
}
killme2008 2003-10-27
  • 打赏
  • 举报
回复
dev-cpp:

int main(int argc, char* argv[])
{
int x,a;
x=a=3,6*a;
printf("x=%d\n",x);
printf("a=%d\n",a);
return 0;
}

测试结果
x=3
a=3
TianGuangZao 2003-10-27
  • 打赏
  • 举报
回复
b=(a=2,a+1) 绝对是定义了的。

On the other hand, it can be used within a parenthesized expression or within the second
expression of a conditional operator in such contexts. In the function call

f(a, (t=3, t+2), c)

the function has three arguments, the second of which has the value 5.

写的很清楚。

to:wingfiring(别逗了)
If an attempt is made to modify the result of a comma operator or to
access it after the next sequence point, the behavior is undefined.
有断章取义嫌疑。

”如果在逗号表达式中试图修改后继的表达式,结果都是未定义的。“
这样的翻译可不敢恭维。
wingfiring 2003-10-26
  • 打赏
  • 举报
回复
to maddevil()
If an attempt is made to modify the result of a comma operator or to
access it after the next sequence point, the behavior is undefined.

这不是说得很清楚了吗?the behavior is undefined!
如果在逗号表达式中试图修改后继的表达式,结果都是未定义的。

作为一个原则,除了&&和||,任何复合表达式的求值顺序都是未定义的,任何关于计算顺序的假设都是错误的。错误的根本原因在于副作用,=,++,--都是有副作用的,如果表达式的最终结果可能受到副作用的影响,则表达式的值就是无意义的。

举例来说:
i++;
i++,j, j+k
表达式虽然也有副作用,但是,它没有影响到表达式的值,所以是确定的结果。
i=3, j*i;

这个有问题,j*i依赖于i,但是i的发生了副作用。
foreversq 2003-10-25
  • 打赏
  • 举报
回复
第二个问题是18
maddevil 2003-10-25
  • 打赏
  • 举报
回复
到底是怎么一个样子?谁出来说清楚.
我真的好想知道到底是谁错了
我觉得我是对的,可还是很不放心,所以很想搞清楚
maddevil 2003-10-25
  • 打赏
  • 举报
回复
to wingfiring(别逗了):

关于逗号运算符标准是有规定的
以下是标准里关于运算符的描述: (我英文不好,难道是我没看懂??还是别的什么情况,请指教)
ISO/IEC 9899:1999 (E) ©ISO/IEC

6.5.17 Comma operator

Syntax

1 expression:
assignment-expression
expression , assignment-expression
Semantics

2 The left operand of a comma operator is evaluated as a void expression; there is a
sequence point after its evaluation. Then the right operand is evaluated; the result has its
type and value.94) If an attempt is made to modify the result of a comma operator or to
access it after the next sequence point, the behavior is undefined.

3 EXAMPLE As indicated by the syntax, the comma operator (as described in this subclause) cannot
appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists
of initializers). On the other hand, it can be used within a parenthesized expression or within the second
expression of a conditional operator in such contexts. In the function call

f(a, (t=3, t+2), c)

the function has three arguments, the second of which has the value 5.
killme2008 2003-10-25
  • 打赏
  • 举报
回复
赞成一楼的
maddevil 2003-10-25
  • 打赏
  • 举报
回复
就是12和16啊 难道你不是这样吗?!

#include <stdio.h>
#include <conio.h>
int main(void)
{

int i = 10;
int j,k;

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


printf("j=%d\n",j);
printf("k=%d\n",k);

getch();

}
ecstar 2003-10-25
  • 打赏
  • 举报
回复
我在vc和dev-cpp中测试过竟然都是3
???
加载更多回复(18)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧