When can I omit curly braces in C?

后端 未结 9 1933
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 09:14

I\'m almost certain this has been asked before, but I can\'t find it being answered anywhere.

When can I omit curly braces in C? I\'ve seen brace-less return

相关标签:
9条回答
  • 2020-11-29 10:11

    But this doesn't always seem to work for all statements.

    Specifically? When a single statement is expected, then it's perfectly valid. Loops, the if statement, etc. all expect a statement and that's either a block, or, well, a single statement without being enclosed in a block.

    0 讨论(0)
  • 2020-11-29 10:12

    If you look at the C syntax, there are a number of contexts that require a statement, a term that's defined by the grammar itself.

    In any of those contexts, one of forms of statement you can use is a compound-statement, which consists of an opening brace {, a sequence of zero or more declarations and/or statements, and a closing brace }. (In C90, all declarations in a compound-statement must precede all statements; C99 removed that restriction.)

    A function-definition specifically requires a compound-statement, not just any kind of statement. (I think that's the only case where a compound-statement is the only kind of statement you can use). If not for that restriction, you'd be able to write:

    void say_hello(void) printf("Hello, world\n");
    

    But since most function definitions contain multiple declarations and/or statements, there wouldn't be much advantage in permitting that.

    There's a separate question: when should you omit braces. In my personal opinion, the answer is "hardly ever". This:

    if (condition)
         statement;
    

    is perfectly legal, but this:

    if (condition) {
        statement;
    }
    

    IMHO reads better and is easier to maintain (if I want to add a second statement, the braces are already there). It's a habit I picked up from Perl, which requires braces in all such cases.

    The only time I'll omit the braces is when an entire if statement or something similar fits on a single line, and doing so makes the code easier to read, and I'm unlikely to want to add more statements to each if:

    if (cond1) puts("cond1");
    if (cond2) puts("cond2");
    if (cond3) puts("cond3");
    /* ... */
    

    I find such cases are fairly rare. And even then, I'd still consider adding the braces anyway:

    if (cond1) { puts("cond1"); }
    if (cond2) { puts("cond2"); }
    if (cond3) { puts("cond3"); }
    /* ... */
    
    0 讨论(0)
  • 2020-11-29 10:12

    You mainly need curly braces when you want to combine multiple statements or expressions into one, e.g.:

    {
      x = 1;
      y = 2;
    }
    

    So if you put the above under if or else the whole thing in the braces will be executed as a whole, whereas if you omit the braces, only the first one (x = 1; in this case) will be used as part of if or else.

    You also typically use them with switch():

    switch (x)
    {
    case 1:
      // do smth
      break;
    case 2:
      // do smth else
      break;
    }
    

    You typically need them with the do-while statement:

    do
    {
      printf("%d\n", x);
      x++;
    } while (x < 10);
    

    You need them with C89 compilers when you want to define and use a temporary variable in the middle of the code:

    int main(void)
    {
      puts("Hello World!");
      {
        int x;
        for (x = 0; x < 10; x++)
          printf("%d\n", x);
      }
      return 0;
    }
    

    You use them to begin and end the body of a function, a structure/union definition, an enumeration definition, initialization of a structure/union/array, e.g.:

    void blah(void)
    {
    }
    
    enum e
    {
      e1 = 1,
      e2 = 2
    };
    
    struct s
    {
      int foo;
      int bar;
    } s = { 1, 2 };
    
    int a[3] = { 1, 2, 3 };
    

    You may omit them sometimes in initializations, e.g.:

    char str2[] = { "abc" };
    char str1[] = "abc";
    
    0 讨论(0)
提交回复
热议问题