Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 什么是逗号分隔的作业集?_C - Fatal编程技术网

C 什么是逗号分隔的作业集?

C 什么是逗号分隔的作业集?,c,C,我注意到在日常生活中 else *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); 它为什么有效 它做什么?逗号运算符是一个序列点:每个逗号分隔的表达式都从左到右求值。结果具有右操作数的类型和值。从功能上讲,您的示例相当于(更具可读性?): 以下是本标准为逗号运算符(6.5.17)提供的另一个示例: 在函数调用中 f(a, (t=3, t+2), c) 该函数有三个参数

我注意到在日常生活中

else 
  *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
它为什么有效


它做什么?

逗号运算符是一个序列点:每个逗号分隔的表达式都从左到右求值。结果具有右操作数的类型和值。从功能上讲,您的示例相当于(更具可读性?):

以下是本标准为逗号运算符(6.5.17)提供的另一个示例:

在函数调用中

f(a, (t=3, t+2), c)
该函数有三个参数 其中第二个的值为5

来自维基百科:

在C和C++编程语言中,逗号运算符(由令牌表示)是一个二进制算符,它计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)。逗号运算符的优先级低于任何C运算符,并充当序列点

逗号标记作为运算符的使用不同于它在函数调用和定义、变量声明、枚举声明和类似构造中作为分隔符的使用

在本例中,第二行和第三行之间的不同行为是由于逗号运算符的优先级低于赋值

int a=1, b=2, c=3, i;   // comma acts as separator in this line, not as an operator
i = (a, b);             // stores b into i                                ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;     ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a = 5 into i     ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i                                ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i                                ... a=5, b=2, c=3, i=3

链接:

为什么它不应该工作?它将
%
设置为_hex(*pstr>>4)
设置为_hex(*pstr&15)
设置为由
pbuf
寻址的顺序内存块。等效代码可以是以下代码:

else {
  *pbuf = '%';
  *(pbuf + 1) = to_hex(*pstr >> 4);
  *(pbuf + 2) = to_hex(*pstr & 15);
  pbuf += 3;
}

i=a,b,在
b
上发生了什么?在这种情况下,为什么要写
b
else {
  *pbuf = '%';
  *(pbuf + 1) = to_hex(*pstr >> 4);
  *(pbuf + 2) = to_hex(*pstr & 15);
  pbuf += 3;
}