关于我对输出的确定。它是 1 40 1 在使用 C 时,它显示输出为 0 41 1 这怎么可能?我要进入什么错误步骤?
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,a,b;
n = 400;
a = n % 100; //remainder operation
b = n / 10; //division operation
n = n % 10; //remainder operation
printf("%d %d %d",n++,++b,++a); //post-,pre-,pre- increment used
getch();
}
最佳答案
您的编译器打印的内容是正确的。程序流程如下:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,a,b;
n = 400; // n has value 400
a = n % 100; // a has value 0
b = n / 10; // b has value 40
n = n % 10; // n has value 0
// n++ evaluates to 0, afterwards n has the value 1
// ++b evaluates to 41, afterwards b has the value 41
// ++a evaluates to 1, afterwards a has the value 1
printf("%d %d %d",n++,++b,++a);
// Thus, 0 41 1 is printed.
getch();
}
特别注意后缀增量运算符n++
返回n
的值不变,然后改变n
。这就是为什么 0
打印在第一列中。
关于c - 使用 Turbo C++ 3.0 编译器的 C 语言程序的令人困惑的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33552385/