Conditional operator “ ? : ”

限于喜欢 提交于 2019-12-01 04:50:20

问题


I've done my programming exam in C yesterday. There was a question I could not answer, and even though I've studied today I can't come up with a solution.

So we have this:

int A= -1 , B= -2, C= -3, X=1;
X = B != C ? A=(~C) - A-- : ++C + (~A);
printf("A= %d  B= %d  C =%d  X=%d \n", A,B,C,X);

I know this operator functions if X = B != C is true then A=(~C) - A-- is executed. If it's false, ++C + (~A) is executed.

Can anyone tell me and explain what are the values of A, B, C and X in that printf?

NEW

This was included in a question that asks to do a "trace" to the whole program:

     #include <stdio.h>
            void main(){
            int A= -1 , B= -2, C= -3, X=1;

        X = B != C ? A=(~C) - A-- : ++C + (~A);
        printf("A= %d  B= %d  C =%d  X=%d \n", A,B,C,X);

if(~A){
        printf("\n out1\n");
        C= A | B
        printf("A= %d  B= %d  C =%d  X=%d \n", A,B,C,X);
        C= C <<1;}

if(A^B){
         printf("\n out2\n");
        C= B & A
        B += 2
        X= X>>1
        printf("A= %d  B= %d  C =%d  X=%d \n", A,B,C,X);

By the way can anyone tell me what does it mean those if conditions?


回答1:


The statement

X = B != C ? A=(~C) - A-- : ++C + (~A);

is equivalent to

if(B != C)
    X = (A = (~C) - (A--));
else 
    X = ++C + (~A);

So, the expression A = (~C) - (A--) invokes undefined behavior. In this case nothing good can be expected.

That said, this is a faulty question and shouldn't be asked in an examination. Or it could be asked with multiple choice answers as long as one option states that the code will invoke undefined behavior.




回答2:


This question should never be on an exam, because it contains undefined behavior.

Specifically, this assignment A = (~C) - A-- modifies A twice - in the -- compound assignment, and in the assignment itself. Since there is no sequence point in between the two, the behavior is undefined.

Note: This does not mean that the program is not going to print anything. It would most definitely produce some output on most platforms. However, none of that matters, because C the program is invalid in its entirety: it can produce any output it chooses to, produce no output, or even crash.




回答3:


Why not to surprise your teacher with very detailed explanation of what is happening ;)

    ...
    mov     eax, dword ptr [rbp - 16]       ; get C
    xor     eax, -1                         ; negate C
    mov     ecx, dword ptr [rbp - 8]        ; get A
    mov     edx, ecx                        ; put A into edx
    add     edx, -1                         ; add -1 to edx => A--
    mov     dword ptr [rbp - 8], edx        ; store result inside A
    sub     eax, ecx                        ; substract from ~C what was the result of A--
    mov     dword ptr [rbp - 8], eax        ; store it inside variable A
    ...


来源:https://stackoverflow.com/questions/44831643/conditional-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!