When can I omit curly braces in C?

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?


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.


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 the 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'm fairly sure 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"); }
/* ... */

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.


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