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

纵然是瞬间 提交于 2019-11-28 12:05:06

问题


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'm a bit confused, as it seems like Brian W. Kernighan/Dennis M. Ritchie's book "The C Programming Language" doesn't use a lot of curly braces, where I would assume it's normal to use them (from a Java perspective). E.g. 1.6 in the book where the code is:

while((c = getchar())) != EOF)
    if (c >= '0' && c <= '9')
        ++ndigit[c-'0'];
    else if() /*and so forth...*/
        ++nwhite;
    else
        ++nother;

From a Java perspective I'm used to that only the first statement would be run then, because of the lack of curly braces, but the indentation suggests that everything will run (if, else if and else).

So what I'm asking is: What would run here, and why would it run? Are if, else if and else all in the scope of the while loop? Are there any conventions to refer to, that I can read to try to understand it better? Thanks in advance.


回答1:


The while, if, else if, and else are followed by a single statement. This statement can be an actual line of C, or a block statement (surrounded by braces). The if, else if, and else are considered as one block.

So using braces, this would be the equivalent:

while((c = getchar())) != EOF) {
    if (c >= '0' && c <= '9') {
        ++ndigit[c-'0'];
    }
    else if() {  /*and so forth...*/
        ++nwhite;
    }
    else {
        ++nother;
    }
}



回答2:


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.




回答3:


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.




回答4:


If an if statement does not contain a curly brace, you can inline up to one semi colon after, and it will automatically assume the curly braces as such:

while((c = getchar())) != EOF){
    if (c >= '0' && c <= '9')
    {
        ++ndigit[c-'0'];
    }
    else if() /*and so forth...*/
    {
        ++nwhite;
    }
    else
    {
        ++nother; 
    }
 }

So this is valid:

 while(true) i++;

And this should not be valid:

 while(true) i++; break; printf("hello");

 if(true) printf("Hello"); break; 


来源:https://stackoverflow.com/questions/18082135/how-do-curly-braces-and-scope-w%d0%berk-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!