6

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?

dbush
  • 205,898
  • 23
  • 218
  • 273
WhiteNoise
  • 71
  • 3

4 Answers4

12

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    This is called the dangling `else` ambiguity and compilers are built to match the `else` with the closest `if`. – Paul Ogilvie Nov 13 '18 at 15:00
  • Alright that seem very good, and it's probably that; but maybe something that gives a compile error? Edit: I was also thinking about declaring a variable on a "statement-only-if", that would just basicly destroy the new variable, making it useless. – WhiteNoise Nov 13 '18 at 15:02
  • @cmaster Ok, thanks. I'm pretty sure it's the dangling else then, since we were also talking about nestes if statements. – WhiteNoise Nov 13 '18 at 15:20
  • @WhiteNoise Btw: google "goto fail bug" for a high-profile, real world example ^^ – cmaster - reinstate monica Nov 13 '18 at 15:26
  • @WhiteNoise Forget about my comment about compiler error not possible with an `if()`. It's wrong. See Antti Haapala's answer (https://stackoverflow.com/a/53284483/2445184) for details. I'm going to delete my wrong comment now. – cmaster - reinstate monica Nov 14 '18 at 10:14
2

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

1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

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.

bhanu7k
  • 65
  • 9