相关疑难解决方法(0)

为什么这些构造使用前后增量未定义的行为?

#include <stdio.h>

int main(void)
{
   int i = 0;
   i = i++ + ++i;
   printf("%d\n", i); // 3

   i = 1;
   i = (i++);
   printf("%d\n", i); // 2 Should be 1, no ?

   volatile int u = 0;
   u = u++ + ++u;
   printf("%d\n", u); // 1

   u = 1;
   u = (u++);
   printf("%d\n", u); // 2 Should also be one, no ?

   register int v = 0;
   v = v++ + ++v;
   printf("%d\n", v); // 3 (Should be the …
Run Code Online (Sandbox Code Playgroud)

c increment operator-precedence undefined-behavior sequence-points

793
推荐指数
13
解决办法
7万
查看次数

逗号运算符如何工作

逗号运算符如何在C++中工作?

例如,如果我这样做:

a = b, c;  
Run Code Online (Sandbox Code Playgroud)

最终是否等于b或c?

(是的,我知道这很容易测试 - 只是在这里记录,以便有人快速找到答案.)

更新: 此问题在使用逗号运算符时暴露了细微差别.只是记录下来:

a = b, c;    // a is set to the value of b!

a = (b, c);  // a is set to the value of c!
Run Code Online (Sandbox Code Playgroud)

这个问题实际上是受到代码中的拼写错误的启发.打算做什么

a = b;
c = d;
Run Code Online (Sandbox Code Playgroud)

转换成

a = b,    //  <-  Note comma typo!
c = d;
Run Code Online (Sandbox Code Playgroud)

c++ comma-operator

165
推荐指数
6
解决办法
5万
查看次数

X = X ++之间有什么区别; vs X ++ ;?

你以前试过这个吗?

static void Main(string[] args)
{
    int x = 10;
    x = x++;
    Console.WriteLine(x);
}
Run Code Online (Sandbox Code Playgroud)

产量:10.

但对于

static void Main(string[] args)
{
    int x = 10;
    x++;
    Console.WriteLine(x);
}
Run Code Online (Sandbox Code Playgroud)

产量:11.

谁能解释为什么这个?

c#

52
推荐指数
5
解决办法
4万
查看次数