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?
You can omit them when there's a single statement you're executing:
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):
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: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 theif
it's expected a statement, thus you can skip the curly brackets.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:
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:
is perfectly legal, but this:
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
:I find such cases are fairly rare. And even then, I'd still consider adding the braces anyway:
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.The following seems a little tricky:
I guess the C preprocessor takes care of empty lines, so this statement is fine. Very bad practice of course.
An example:
you can use the valid equivalent form:
Some verbose compilers may warn, but it is valid.
Note that for the
if
statement (same for thewhile
, thefor
statement, ...) the{}
are not omitted. They are just not requested. Theif
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
{}
.The only places you can omit brackets are for the bodies of
if-else
,for
,while
, ordo-while
statements if the body consists of a single statement:However, note that each of the above examples counts as single statement according to the grammar; that means you can write something like
This is perfectly legal;
if (cond2) do_something();
reduces to a single statement. So, for that matter, doesif (cond1) if (cond2) do_something();
, so you could descend further into madness with something likeDon't do that.