无法理解以下代码的输出

时间:2019-06-25 17:05:52

标签: c operators

下面给出的代码是我的老师问的。 只是测试谁可以在不运行代码的情况下弄清楚输出。 用于编译Turbo C ++。

在下面的代码中,我无法理解函数调用中增量和减量是如何发生的,天气值是从左向右或从右向左传递的。

#include<stdio.h>
#include<conio.h>

int main(){
    char c[]="hello";
    char *p;
    clrscr();
    p=&c[0];
    printf("%c %c %c ",++*p,*++p,*p++);
    puts(c);
    getchar();
    return 0;
}

根据我的输出是: 伊尔 艾姆洛

但是当它编译并运行时我得到了: 升 hem 为什么会发生这种情况,即值是从右向左传递还是其他?

谢谢。

1 个答案:

答案 0 :(得分:0)

运行OP发布的代码会导致:

m l h hemlo

但是,编译器输出为:

gcc    -ggdb -Wall -o "untitled" "untitled.c"  -lncurses -lrt -lpthread -lm -lgmp 
untitled.c: In function ‘main’:
untitled.c:11:32: warning: operation on ‘p’ may be undefined [-Wsequence-point]
printf("%c %c %c ",++*p,*++p,*p++);
                              ~^~
untitled.c:11:32: warning: operation on ‘p’ may be undefined [-Wsequence-point]
Compilation finished successfully.

由于未定义的问题,实际输出可能是任何东西

相关问题