Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C allow leaving out braces around blocks? [closed]

Most of us have probably encountered the bug of when you add another statement inside an 'if' and notice that it prints regardless of the condition of the branch, only to find out sooner or later in frustration that the braces around the 'if' condition was missing.

Example:

if (condition) {
    statement;
}

Compared to:

if (condition)
    statement;

Please note that I wouldn't want a discussion on what coding standard is better, as we all have our opinion about that and it's very much dependent on context. Instead, I would be interested in the advantages from a programming language point of view and from these advantages, why it was decided not to be strictly enforced. Does it make for a simpler grammar? Compiler implementation?

What is the advantage of leaving it optional, rather than strictly enforcing it? What does the standard say about it?

like image 591
Andreas Avatar asked Sep 14 '15 18:09

Andreas


People also ask

Why do we use block of statements with braces in C?

Braces improve the uniformity and readability of code. More important, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.

What do curly brackets do in C?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.

DO IF statements need curly braces C?

Yes it is not necessary to use curly braces after conditions and loops (functions always need them) IF and only if there is just one statement following. In this case it automatically uses the next statement.


2 Answers

The C Standard says that a if (expression) must be followed by a statement. It does not require the statement to be a compound statement.

In the "Spirit of C" (see C Rationale document), one of the phrase is:

Trust the programmer.

MISRA-C on the other hand has a required rule for if and loops that the statement has to be a compound statement.

Some coding styles allow a single statement for if only if the statement is put in the same line as the if. For example:

if (condition) statement;

Note that C and C++ are not only the languages to have the {} be optional in if. For example in Java it is also optional, but in Perl it is required.

like image 159
ouah Avatar answered Oct 20 '22 05:10

ouah


Because:

  • that would be excessive meddling — the programmer chooses coding style, not the language;

  • it would further complicate the grammar; easier and more robust to just say that an if (condition) is followed by a statement and let the recursion handle everything.

Personally I find the distrust of this construct to be largely without merit for any vaguely competent programmer; I've not once encountered this "bug" where I've forgotten how to write C++ just because I chose to omit a brace pair. If you come across it, I suggest you treat it as a cue to pay more attention to what you're doing, rather than masking it with "tricks" and "style guides".

like image 41
Lightness Races in Orbit Avatar answered Oct 20 '22 06:10

Lightness Races in Orbit