简体   繁体   中英

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?

#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 . That's why 0 is printed in the first column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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