How do curly braces and scope wоrk in C?

后端 未结 4 2130
野的像风
野的像风 2020-12-22 07:04

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\'

4条回答
  •  有刺的猬
    2020-12-22 07:23

    You may just treat if/else if/else instructions as one block - they can't really be divided, else cannot exist on it's own.

    On the side note, it's sometimes confusing when you have a situation like

    if(something)
       if (somethingOther)
         ....
    else
       .....
    

    If your code is long enough, you might get confused where to attach this else, so it's good to always use braces. As stated in comment, else always attaches to the "nearest" if.

提交回复
热议问题