Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

syntax

if(...) {
  ...
}

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

like image 757
cpuer Avatar asked Jun 15 '11 03:06

cpuer


People also ask

Is semicolon mandatory in C?

Semicolon there is NOT optional.

Which loop needs a semicolon after in C?

while' loop syntax needs a semicolon at the end. Whereas for and while loop do not need a semi-colon terminator at end.

Why do we need to give a semicolon after a structure declaration?

You have to put the semicolon there so the compiler will know whether you declared any instances or not. This is a C compatibility thing. Doesn't even need the name or constituents: class { } waldo; is legal.

What does the semicolon do in C?

Role of Semicolon in C: Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements.


1 Answers

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);
like image 159
iammilind Avatar answered Sep 24 '22 07:09

iammilind