Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在什么情况下可以省略C中的大括号?_C - Fatal编程技术网

在什么情况下可以省略C中的大括号?

在什么情况下可以省略C中的大括号?,c,C,我正在读著名的K&R书籍,被1.6中的例子绊倒了 #include <stdio.h> /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = get

我正在读著名的K&R书籍,被1.6中的例子绊倒了

#include <stdio.h>
/* count digits, white space, others */
main()
{
    int c, i, nwhite, nother;
    int ndigit[10];
    nwhite = nother = 0;

    for (i = 0; i < 10; ++i)
        ndigit[i] = 0;

    while ((c = getchar()) != EOF)
        if (c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;

    printf("digits =");

    for (i = 0; i < 10; ++i)
        printf(" %d", ndigit[i]);

    printf(", white space = %d, other = %d\n", nwhite, nother);
}
#包括
/*计算数字、空白、其他*/
main()
{
int c,i,nwhite,other;
int-ndigit[10];
nwhite=nother=0;
对于(i=0;i<10;++i)
ndigit[i]=0;
而((c=getchar())!=EOF)

如果(c>='0'&&cwhile
语句的语法为:

while ( expression ) statement
在C语法中,语句为:

statement:
    labeled-statement
    compound-statement
    expression-statement
    selection-statement
    iteration-statement
    jump-statement
选择语句定义为:

selection-statement:
    if ( expression ) statement
    if ( expression ) statement else statement
    switch ( expression ) statement
正如你所看到的,
if
else if
else
本身就是一个语句

您可能还注意到,复合语句本身就是一条语句,因此,例如,此块:

{
    statement1;
    statement2;
}

也是一条语句。

如果从表达式中删除大括号,则表达式将仅对一条语句执行。

好的,If/else的整个链只是一条语句。如果构造
If…else If…else
不是一条语句,则表示至少有两条语句。因此你可以在这两个语句之间插入任意多的分号。你可以在构造中的任何位置插入额外的分号吗?试试看。谢谢你的解释!这对我来说非常清楚。我假设你是从一个C标准得到的;如果是,这是否意味着
或者如果
本身不是一个“关键字”在C语言中,省略花括号只是一个相当方便的结果?@Frxstrem
else如果
不是关键字,它是一个
else
,后面跟着一个
if
,这在C语言中是允许的。这是不清楚、不明确的,可能是不正确的