samedi 21 novembre 2020

How "else" works in "1 line if" in C?

I searched it up but I haven't found the answer to this, so I saw a piece of code as follows,

int a=0, b=1, c=0;
if (a)
if (b && c) puts ("yes");
else puts ("no");

I know that we may not use {} if the statement is only 1 line like if (true) //something;, so I assumed that else belonged to if (a) but when I run the code, no doesn't show up, at first I thought it's the same as

if (a) if (b && c) puts ("yes");
else puts ("no");

or,

if (a)
    if (b && c) puts ("yes");
else puts ("no");

But instead, it is

if (a)
{
    if (b && c) puts ("yes");
    else puts ("no");
}

Why that else is paired with the inner if instead of the outer if?

Aucun commentaire:

Enregistrer un commentaire