mercredi 7 juin 2017

Short-circuit evaluation evaluating if( (a = 4) || (b = 6) || (c = 7) || (d = 8) )

The program is posted below:

#include <stdio.h>

int main(void)
{
    int a, b, c, d;

    printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

    if( (a = 4) || (b = 6) || (c = 7) || (d = 8) )
        printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
}

I understand the first printf statement but in the 2nd if statement I do not understand what it would evaluate when there is only one equal sign instead of two.

The output is :

        a = 0, b = 0, c = 32767, d = -341260496
        a = 4, b = 0, c = 32767, d = -341260496

So the 2nd if statement ended up being true but how?
I thought one equal sign would be assigning a value to the variables.

Aucun commentaire:

Enregistrer un commentaire