Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I omit curly braces in C?

Tags:

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?

like image 862
ucarion Avatar asked Feb 15 '13 19:02

ucarion


2 Answers

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.

like image 69
John Bode Avatar answered Sep 30 '22 19:09

John Bode


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"); } /* ... */ 
like image 21
Keith Thompson Avatar answered Sep 30 '22 18:09

Keith Thompson