When is a semicolon after } mandated in c/c++?

10,964

Solution 1

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);

Solution 2

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

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

Share:
10,964
cpuer
Author by

cpuer

Updated on August 04, 2022

Comments

  • cpuer
    cpuer over 1 year
    if(...) {
      ...
    }
    

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

  • cpuer
    cpuer almost 13 years
    Why ; is required after class declaration but not after function declaration? C++ parser can't parse it without the ; in that case?
  • cpuer
    cpuer almost 13 years
    any rational behind all these?
  • Billy ONeal
    Billy ONeal almost 13 years
    @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.
  • Luc Danton
    Luc Danton almost 13 years
    @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.
  • Billy ONeal
    Billy ONeal almost 13 years
    @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 :)
  • iammilind
    iammilind almost 13 years
    @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.
  • rubenvb
    rubenvb almost 13 years
    a do-while needs a semi-colon at the end!
  • rubenvb
    rubenvb almost 13 years
    Don't forget a do{ ... } while(...);
  • Chris Dodd
    Chris Dodd almost 13 years
    @rubenvb: if you try to put a ; after the } in a do-while loop, you'll get a syntax error.
  • rubenvb
    rubenvb almost 13 years
    but it is needed after the final ).
  • tejasvi88
    tejasvi88 over 2 years
    @AdamRosenfield Are your aware of any other constructs like if-else, do-while and try-catch where semicolon after brace can be illegal?