When can I omit curly braces in C?

2020-02-10 14:49发布

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 statements before, such as

if (condition)
  return 5;

But this doesn't always seem to work for all statements, i.e. when declaring a method.

edit:

Are the rules for brace omission the same as in Java?

标签: c
9条回答
三岁会撩人
2楼-- · 2020-02-10 14:51

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-10 14:53

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"); }
/* ... */
查看更多
4楼-- · 2020-02-10 14:53

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.

查看更多
手持菜刀,她持情操
5楼-- · 2020-02-10 14:54

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.

查看更多
6楼-- · 2020-02-10 14:55

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 {}.

查看更多
你好瞎i
7楼-- · 2020-02-10 15:05

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.

查看更多
登录 后发表回答