c - 使用 Turbo C++ 3.0 编译器的 C 语言程序的令人困惑的输出

标签 c

关于我对输出的确定。它是 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/

相关文章:

命令行加号程序产生错误结果

c++ - 求复数的指数

c++ - static 关键字会影响作用域吗?

c - 在 C 中没有正确使用 strcmp

c - 如何调试驱动程序加载错误?

c - 函数返回时堆栈会发生什么?

c - 为什么在我的程序中从 float 到 double 的隐式转换会返回无意义的数字?

c - ID 的 Flex 模式给出 'Segmentation fault'

c - 如何将原始套接字绑定(bind)到特定端口?

c++ - 从守护进程运行 Linux 命令