vendredi 1 avril 2016

if(x=y){...} else{...} takes else path

I know it's a basic and trivial questions but when coding in C, why doesn't it go into the second if-statement? I know there's only one equal sign, so this should set x = 0 and then go into the code? Is it because the compiler understands x = 0 as False so it doesn't enter the chunk? And why does it execute the else statement? Thank you!

#include <stdio.h>

void main()
{
    int x=1, y=0;

    if (x >= y){
       x = 0;

    if (x = y)
       x = x + 1;

    else{
       x = 4;
       printf(" %d",x);
    }
}

Aucun commentaire:

Enregistrer un commentaire