简体   繁体   English

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

[英]Perplexing Output of C language program using Turbo C++ 3.0 compiler

Concerning my determination for output.It's 1 40 1 While using C it displays output as 0 41 1 How's that possible?What wrong step I'm going into? 关于我对输出的确定。它是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(); 
}

What your compiler prints is correct. 您的编译器打印的内容正确。 Here is the program flow: 这是程序流程:

#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(); 
}

Notice especially that the postfix-incrememnt operator n++ returns the value of n unchanged and then changes n . 通知特别是后缀-incrememnt操作者n++返回的值n不变,然后改变n That's why 0 is printed in the first column. 这就是为什么在第一列中打印0的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM