Conditional operator "?:"

I did my C programming exam yesterday. There was a question that I could not answer, and although I studied today, I can not 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 function, if X = B != Ctrue, then it is executed A=(~C) - A--. If it is erroneous, it is executed ++C + (~A).

Can someone tell me and explain what are the meanings of A, B, C and X in this printf?

NEW

This was included in a question that asks for a “trace” for the entire 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 that means, those ifconditions?

+4
source share
3 answers

Statement

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

equivalently

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

, A = (~C) - (A--) undefined. .

, , . , , undefined.

+8

, undefined.

, A = (~C) - A-- A - -- . , undefined.

: , . , , . , C : , , .

+3

Why not surprise your teacher with a 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
    ...
0
source

Source: https://habr.com/ru/post/1680436/


All Articles