Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon in C++?

Tags:

c++

c

Is the "missing semicolon" error really required? Why not treat it as a warning?

When I compile this code

int f = 1 int h=2; 

the compiler intelligently tells me that where I am missing it. But to me it's like - "If you know it, just treat it as if it's there and go ahead. (Later I can fix the warning.)

  int sdf = 1, df=2;   sdf=1 df =2 

Even for this code, it behaves the same. That is, even if multiple statements (without ;) are in the same line, the compiler knows.

So, why not just remove this requirement? Why not behave like Python, Visual Basic, etc.

Summary of discussion

Two examples/instances were missing, and a semi-colon would actually cause a problem.

1.

return  (a+b) 

This was presented as one of the worst aspects of JavaScript. But, in this scenario, semicolon insertion is a problem for JavaScript, but not for C++. In C++, you will get another error if ; insertion is done after return. That is, a missing return value.

2

int *y; int f = 1 *y = 2; 

For this I guess, there is no better way than to introduce as statement separator, that is, a semicolon.

like image 675
SysAdmin Avatar asked May 05 '10 07:05

SysAdmin


People also ask

What is the use of semicolon and curly braces in C?

The semi colon is “separater”. The simple answer is to tell the compiler that which line of code is separate each other then you get the desired output otherwise if you not use ; then compiler think all line are one and then confused and gave the many errors and errors.

Do you need a semicolon after break in C?

Control-flow changing statements like break , continue , goto , return , and throw must have semicolons after them. Declaration statements like function prototypes, variable declarations, and struct/class/union declarations must be terminated with semicolons.

What does semi colon mean in for loop in C?

Semicolon is a legitimate statement called null statement * that means "do nothing". Since the for loop executes a single operation (which could be a block enclosed in {} ) semicolon is treated as the body of the loop, resulting in the behavior that you observed.

What does two semicolons mean in C?

A "double semicolon" does not have any special meaning in c. The second semicolon simply terminates an empty statement. So you can simply remove it.


2 Answers

It's very good that the C++ compiler doesn't do this. One of the worst aspects of JavaScript is the semicolon insertion. Picture this:

return   (a + b); 

The C++ compiler will happily continue on the next line as expected, while a language that "inserts" semicolons, like JavaScript, will treat it as "return;" and miss out the "(a + b);".

Instead of rely on compiler error-fixing, make it a habit to use semicolons.

like image 173
Delan Azabani Avatar answered Oct 06 '22 03:10

Delan Azabani


There are many cases where a semicolon is needed.

What if you had:

int *y; int f = 1 *y = 2; 

This would be parsed as

int *y; int f = 1 * y = 2; 

So without the semicolons it is ambiguous.

like image 31
Jason Williams Avatar answered Oct 06 '22 01:10

Jason Williams