When is a semicolon after } mandated in c/c++?
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.
cpuer
Updated on August 04, 2022Comments
-
cpuer over 1 year
if(...) { ... }
It seems in the above case a
;
is optional,when is a semicolon after}
necessary in c/c++? -
cpuer almost 13 yearsWhy
;
is required after class declaration but not after function declaration? C++ parser can't parse it without the;
in that case? -
cpuer almost 13 yearsany rational behind all these?
-
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 namedexample
of typeABC
. -
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 almost 13 years@cpuer: For the
class
andenum
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 almost 13 years@cpuer, as @Luc said, since in
C++
you can declare objects/pointers after theclass/struct/enum
declaration. Putting a;
helps the parser that one has to type theclass
name to declare any object. Since function /namespace' are not type; the
;` after them is not mandatory. -
rubenvb almost 13 yearsa
do-while
needs a semi-colon at the end! -
rubenvb almost 13 yearsDon't forget a
do{ ... } while(...);
-
Chris Dodd almost 13 years@rubenvb: if you try to put a
;
after the}
in ado
-while
loop, you'll get a syntax error. -
rubenvb almost 13 yearsbut it is needed after the final
)
. -
tejasvi88 over 2 years@AdamRosenfield Are your aware of any other constructs like
if-else
,do-while
andtry-catch
where semicolon after brace can be illegal?