Are there “sequence-point” issues with statements like “int a=4,*ptr=&a;” or “x+=4,y=x*2;”?

≯℡__Kan透↙ 提交于 2019-12-05 07:44:55

The comma that separates function arguments in a function call is not a comma operator - it's just punctuation that happens to be spelled in the same way as the comma operator. There's no sequence point between the evaluation of different function arguments, so that's why you get UB in that case.

On the other hand, in your expression:

x+=4,y=x*2;

the comma here is a comma operator, which introduces a sequence point; there's no UB.

In a declaration, the comma between declarators is also not a comma operator; however, the end of a full declarator (a declarator not part of another declarator) does introduce a sequence point, so a declaration like:

int a = 2, b = a + 1;

is not UB.

There is no UB here.
As per C99 standard:

C. Annex C (informative): Sequence points
#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).

Thus there is no comma operator involved upon function call. Despite presence of comma.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!