Home  >  Q&A  >  body text

gcc - a classic interview question on C language under Linux

If you use the GCC compiler to execute the following program under Linux, what is the output result?

#include<stdio.h>
int main(){
    int a=5;
    printf("%d %d %d",a++,a++,++a);
    return 0;
}

The answer is as follows:

In LINUX GCC compiler
7 6 8
In TURBO C
7 6 6

I can understand the answer of 7 6 6, but I really can’t understand why the output of LINUX GCC compiler is 7 6 8. Although I know this is undefiend behavior, I still want to know why the results of 7 6 8 appear.
Original question source: Several classic interview questions in C language under Linux

巴扎黑巴扎黑2671 days ago942

reply all(2)I'll reply

  • 学习ing

    学习ing2017-06-26 11:01:11

    http://blog.csdn.net/laojiu_/...

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-26 11:01:11

    In fact, it is because this is undefined behavior that is not in the specification, so the compiler is correct no matter how it calculates it, and it is correct even if the results obtained by the compiler are not uniform.

    reply
    0
  • Cancelreply