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

后端 未结 4 2156
野的像风
野的像风 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:20

    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;
        }
    }
    

提交回复
热议问题