C ++中的逗号运算符和逗号分隔符 [英] comma operator and comma seperator in c++

查看:108
本文介绍了C ++中的逗号运算符和逗号分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
何时所有逗号都不用作逗号运算符?

逗号(,)什么时候充当运算符,什么时候充当分隔符?它的后果是什么,如果可能的话,请给两个示例都举一些小例子.

when does comma(,) behave as operator and when does it behave as separator?And what are the consequences of it.If possible please give small examples too for both.

推荐答案

逗号在函数调用,函数声明,初始化程序和变量声明中起分隔符的作用:

The comma behaves as a separator in function calls, function declarations, initializers and variable declarations:

f(a, b);
int a[] = {2, 3, 4};
int c = 2, d = 3;

相比之下,当用于分隔语句时,它的行为类似于逗号运算符:

By contrast, when used to separate statements, it behaves as the comma operator:

a = 2, b = 3;
c = (doSomething(), doSomethingElse(), result());

它产生的结果是它最右边的操作数的值,并且执行的顺序保证是从左到右的,这与函数参数的未定义评估顺序相反.

The result it yields is the value of its rightmost operand and the order of execution is guaranteed to be left to right, contrary to the undefined order of evaluation of function arguments.

这篇关于C ++中的逗号运算符和逗号分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆