9
if(...) {
  ...
}

It seems in the above case a ; is optional,when is a semicolon after } necessary in c/c++?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
cpuer
  • 7,413
  • 14
  • 35
  • 39
  • 4
    Semicolon there is NOT optional. This `if(a) if(b) { ... } else { ... }` means one thing, while `if(a) if(b) { ... }; else { ... }` is a syntax error (`else` without matching `if`). – Adam Rosenfield Jun 15 '11 at 04:34
  • @AdamRosenfield Are your aware of any other constructs like `if-else`, `do-while` and `try-catch` where semicolon after brace can be illegal? – tejasvi88 Dec 23 '21 at 18:04

3 Answers3

17
int a[2] = {1,2}, j = 5;

When initialization of array or structure are done with {} all the subsequent variables are declared after ,.

Edit: As you changed your question; ; is mandatory after the class, enum, initialization syntax declarations.

class A {};  // same for `struct
enum E {};   // enum class (c++0x)
int a[] = {1,2};  // array or object initialization

And as per comment by @rubenvb:

do {} while(condition);
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • any rational behind all these? – cpuer Jun 15 '11 at 03:39
  • 4
    @cpuer It's not a rationale but `struct { int i; } s;` is meaningful in C and C++. When you don't need the object(s) (here, `s`), then the `;` remains. – Luc Danton Jun 15 '11 at 03:40
  • 1
    @cpuer: For the `class` and `enum` examples, I explained in comments on Ernest's answer. For the array or object initializations, you need a semicolon simply because they are statements and all statements are terminated by `;`s :) – Billy ONeal Jun 15 '11 at 03:41
  • @cpuer, as @Luc said, since in `C++` you can declare objects/pointers after the `class/struct/enum` declaration. Putting a `;` helps the parser that one has to type the `class` name to declare any object. Since function / `namespace' are not type; the `;` after them is not mandatory. – iammilind Jun 15 '11 at 03:45
  • 3
    Don't forget a [`do{ ... } while(...);`](http://stackoverflow.com/questions/942251/in-c-c-why-does-the-do-whileexpression-need-a-semi-colon) – rubenvb Jun 16 '11 at 17:25
6

A semicolon by itself is an empty statement, and you can add extra ones anywhere a statement is legal. Therfore it would be legal to put a semicolon right after the braces following your if, although it wouldn't be related to the if at all. The only place I can think of where a semicolon is required right after a bracket is after a class declaration in C++.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 2
    Why `;` is required after class declaration but not after function declaration? C++ parser can't parse it without the `;` in that case? – cpuer Jun 15 '11 at 03:38
  • 10
    @cpuer: Because you can declare instances at the definition site. For example: `struct ABC { int foo; int bar; } example;` creates a variable named `example` of type `ABC`. – Billy ONeal Jun 15 '11 at 03:39
3

A semicolon after a close brace is manadatory if this is the end of a declaration. If it is the end of a block statement then no semicolon is needed, and if one is used, it makes an additional empty statement, which may be illegal if this is the middle of a if-else or a do-while (or a try-catch in C++)


Semicolons are required many places not after a close brace (at the end of any statement except a block-statement, at the end of any declaration, even if there is no }, in a for clause, etc, but generally braces are not involved in any of those.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226