Reputation: 71
I am a computer science student, and some time ago our professor explained us that in C language we can remove curly braces when there's only one statement like:
if (a)
do b
but we can't do something like this:
if(a)
do b
do c
because that would be doing more than one statement.
But it also told us that there's an exception about removing curly braces, something we can't do even if it's only one statement. I did a lot of search but the only thing I found is that I can't do that in a do-while loop, but we are talking about if statements, any help?
Edit: we were also talking about nested if statements, maybe it's about that?
Upvotes: 6
Views: 1004
Reputation: 65
It is difficult to debug when you use macros(multi line) with this syntax. You have to look at preprocessed c code to identify the problem.
#define MULTISTATEMENT a=5;\
b=5;
#include <stdio.h>
int main() {
int a = 10, b = 10;
if(a == 10)
MULTISTATEMENT
else
a = 3;
printf("a = %d b = %d\n", a, b);
return 0;
}
This code generates the error. Place {} either around the macro or around if statement to compile this code.
Upvotes: 0
Reputation: 133919
Well, it is always possible to use a single statement without braces. But what if you don't have any statements:
if (ham)
int eggs = spam();
does not compile because if
needs to guard exactly one statement.
if (ham) {
int eggs = spam();
}
does. There is one compound statement with a declaration with an initialization that calls a function; and 0 statements. Not really useful as such of course, because the variable is unused otherwise...
Upvotes: 2
Reputation: 223917
You professor is probably talking about a situation like this:
if (a)
if (b)
printf("a and b\n");
else // this goes with the inner "if"
printf("not a\n");
Contrary to what the indentation suggests, the else
is not associated with the outer if
statement but with the inner if
statement. In this case you need to add curly braces to the body of the outer if
for the else
to be associated properly:
if (a) {
if (b)
printf("a and b\n");
}
else
printf("not a\n");
It's best to always use braces for the bodies of conditional and looping constructs to prevent this kind of ambiguity and the bugs that go with them.
Upvotes: 13
Reputation: 213842
But it also told us that there's an exception about removing curly braces, something we can't do even if it's only one statement.
I believe your professor is trying to teach you best practices. Even though we can write if
statements without curly braces as far as the C language is concened, we should never do so. It is not really a subjective, personal style preference, but a matter of program safety.
Because it is very easy to write bugs like this:
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail; /* MISTAKE! THIS LINE SHOULD NOT BE HERE */
The 2nd goto
will always be executed, which wasn't intended. I dare to state that every single C programmer out there who allows this style, has also written this bug. I sure did now and then myself, before I stopped using that style entirely.
For this safety reason, well-known coding standards such as CERT-C and MISRA-C ban the use of if
etc without curly braces. See CERT-C EXP19-C and MISRA-C:2012 rule 15.6.
Upvotes: 1