I\'m currently trying to learn some C in my spare time. I have some experience in Java, and I\'m therefore used to limit scoping of i.e. variables with curly braces. But I\'
In C, as in Java, an ìf
and a while
has one statement as its body, and if
can have an optional else
clause with one statement as its body. And in most places where you can have one statement, you can have a statement block, which is a list of statements surrounded by curly braces. This is not different in C than in Java.
In order to avoid ambiguity, if there are two ifs and one else, the else is defined to refer to the last one. I. e.
if(a) x if(b) y else z
is defined to mean
if(a) x { if(b) y else z }
and not
if(a) x { if(b) y } else z
Here, x, y, and z are statements - which can be statement blocks as well.
However, having said all this, it is error prone to leave off the braces, and hence many coding guidelines suggest to always use the curly braces.