下面给定代码的输出是什么?

What will be the output of the given code below?

#include<stdio.h>

int main()
{
    int a =0, b=1, c=2;

    *( ( a+1==1) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d \n", a , b, c );

    return 0;
}

任何人都可以向我解释解决方案和输出吗?

您可以将这段代码拆分成更长但可能更易读的代码:

#include<stdio.h>

int main()
{
    int a=0, b=1, c=2;
    int *ptr;

    if (a + 1 == 1) { // this will be true
        ptr = &b;
    }
    else {
        ptr = &a;
    }

    if (a != 0) { // this will be false
        *ptr = b;
    }
    else {
        *ptr = c;
    }

    printf(“%d, %d, %d \n” a , b, c );

    return 0;
}

根据初始值,a + 1 == 1 为真,所以 ptr = &b;.

因为 a=0; 那么 a != 0 将是错误的,因此 *ptr = c;

==> 与 b = c;

相同

所以预期的输出是

0, 2, 2