相关疑难解决方法(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万
查看次数

如何正确使用ctime()来打印不同的时间戳

我期望下面的代码应该打印不同的时间戳t1和t2,但结果显示t1和t2是相同的.我在哪里弄错了?

#include<iostream>
#include<ctime>

using namespace std;

int main()
{
    time_t t1 = time(NULL);
    cout << "time now " << ctime(&t1) << endl;
    time_t t2 = t1 + 10000.0;
    cout << "time now " << ctime(&t1) << endl << " time later " << ctime(&t2) <<endl;
}
Run Code Online (Sandbox Code Playgroud)

结果:

time now Thu Apr 28 20:37:03 2016

time now Thu Apr 28 20:37:03 2016

 time later Thu Apr 28 20:37:03 2016
Run Code Online (Sandbox Code Playgroud)

c++ ctime

5
推荐指数
1
解决办法
730
查看次数

C - printf的输出说明("%d%d \n",k = 1,k = 3);

如何解释下面代码的输出:

include <stdio.h>

int main(void) {
    int k;
    printf("%d %d\n",k=1,k=3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ideone Link

我的想法是1将分配给k变量然后1打印.同样3将分配给k和输出3.

预期产出

1 3
Run Code Online (Sandbox Code Playgroud)

实际产出

1 1
Run Code Online (Sandbox Code Playgroud)

我是从外推

int a;
if (a = 3) { 
    ...
} 
Run Code Online (Sandbox Code Playgroud)

等于

if (3) { 
    ... 
}
Run Code Online (Sandbox Code Playgroud)

请让我知道我哪里错了?

c printf function-calls variable-assignment sequence-points

5
推荐指数
1
解决办法
159
查看次数