When can I omit curly braces in C?

后端 未结 9 1932
佛祖请我去吃肉
佛祖请我去吃肉 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 09:45

    The following seems a little tricky:

    if (...)
    
            printf("I'm conditional\n");
    

    I guess the C preprocessor takes care of empty lines, so this statement is fine. Very bad practice of course.

    0 讨论(0)
  • 2020-11-29 09:46

    Anytime there is a block with more than one line (counting lines as a single statement with a ; )

    Examples:

    for (i = 0; i < j; i++ ) 
       foo(i);
    
    while (1) 
       foo();
    
    if( bool )
       foo();
    
    void cheese()
       printf("CHEESE");
    

    Everything after the ; in the above lines does not count as 'inside' the block as if there were { }.

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

    If in doubt, use braces. They don't "cost" any extra [unless you absolutely use them incorrectly].

    A fairly basic rule is "if you have more than one semicolon at the same indentation, you should have braces". It's not entirely that simple, but as a "one sentence with no ifs, buts or other complications" it will have to do.

    And yes, Java being based on C-syntax has the same fundamental rules - there are possibly some weirdness where Java is different from C or C++, but that's unusual/strange things, not something you'd normally hit when writing typical code [I haven't done much java, so I can't say I've found any of them, but I'm pretty sure there are SOME differences - they are different languages, after all].

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

    You can omit them when there's a single statement you're executing:

    for(...)
     printf("brackets can be omited here");
    
    if(...)
     printf("brackets can be omited here");
    else
     printf("brackets can be omited here too");
    

    etc.. You can always add them in, it never hurts and help to clarify the scope, but you're pretty safe. The only "catch" I can think of is if you attempt to make a declaration in that scope (which is useless by the way):

    if(...)
      int a = 5;
    

    This will cause an error, but that's because you can perform a single statement without curly brackets, not a declaration. Important distinction.

    Technically speaking, you can even do more than one operation without brackets if they're concatenated with the , operator... not that I advice doing it, just worthy of note:

    if(...)
        value++, printf("updated value with brackets");
    

    And to your edit, you can see in the java specification that the rules are the same. I linked specifically to the if section, but you can see after the if it's expected a statement, thus you can skip the curly brackets.

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

    The only places you can omit brackets are for the bodies of if-else, for, while, or do-while statements if the body consists of a single statement:

    if (cond)
      do_something();
    
    for (;;)
      do_something();
    
    while(condition)
      do_something();
    
    do 
      do_something();
    while(condition);
    

    However, note that each of the above examples counts as single statement according to the grammar; that means you can write something like

    if (cond1)
      if (cond2)
        do_something();
    

    This is perfectly legal; if (cond2) do_something(); reduces to a single statement. So, for that matter, does if (cond1) if (cond2) do_something();, so you could descend further into madness with something like

    for (i=0; i < N; i++)
      if (cond1)
        if (cond2)
          while (cond3)
            for (j=0; j < M; j++)
              do_something(); 
    

    Don't do that.

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

    An example:

    int a[2][2] = {{1, 2}, {3, 4}};
    

    you can use the valid equivalent form:

    int a[2][2] = {1, 2, 3, 4};
    

    Some verbose compilers may warn, but it is valid.

    Note that for the if statement (same for the while, the for statement, ...) the {} are not omitted. They are just not requested. The if statement has this syntax:

    if (expression) statement

    If you want a multiple statement instead of a single statement you can use a compound statement which is surrounded {}.

    0 讨论(0)
提交回复
热议问题