tags:

views:

1089

answers:

10
+21  Q: 

semicolon in C++?

Here is the question

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

Why I am asking this stupid question?

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 its like - "If you know it, just treat it as if its 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. i.e 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,vb etc

Summary of discussion

2 examples/instances were missing semi-colon would actually cause a problem.

1.

return
 (a+b)

This was presented as 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. i.e 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 seperator i.e semicolon

+49  A: 

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.

Delan Azabani
+1 it is always good programming practice to be as explicit as possible with your intentions- don't rely on your tools to do it for you.
Sharpie
The Javascript example is the first one that came to my mind as well.
Steve Rowe
So why not add a line continuation char like _ as it is in other languages
SysAdmin
+1. Good example.
Joe Gauterin
@SysAdmin: Then you'd need to add line continuation characters all over the place, and if you forgot them your code would still correctly compile but wouldn't do what you expected it to.
Joe Gauterin
@SysAdmin Because C++ is not those other languages. It has its very well defined semantics that people can rely on.
Daniel Daranas
@SysAdmin: Coffee or tea?
KennyTM
Why should? Thats the same as asking why should there be a semi-colon delimiter in C and C++.
ChrisBD
@Joe Gauterin - do you see line continuation char's all over the place in VB or python? why are they confident that they dont need ;? I would say that the answer given by Jason Williams is more obvious reason
SysAdmin
Moreover, this particular example code wont compile and run because, you will get another error saying "you are not returning any value for this function as the function expects a return int value"
SysAdmin
@Sys: you can omit the return value for `main`.
Dennis Zickefoose
@Dennis Zickefoose - if you put a return statement in main, you will get an error
SysAdmin
@Sys: I double checked, and you are correct, my apologies. Not returning is the same as returning 0, but `return` is not.
Dennis Zickefoose
@SysAdmin: You're not talking about designing a new language with significant whitespace like python - you're talking about retrofitting them to an existing syntax for which they're not suitable.
Joe Gauterin
@Joe Gauterin - I agree, but I just wanted to say that this scenario presented in this post cannot be compiled for a different reason - i.e function expects an int value as return . if you do ; insertion after return, you will get this error
SysAdmin
C++ does have a line continuation character: \
graham.reeds
+6  A: 

First, this is only a small example; are you sure the compiler can intelligently tell you what's wrong for more complex code? For any piece of code? Could all compilers intelligently recognize this in the same way, so that a piece of C++ code could be guaranteed portable with missing semicolons?

Second, C++ was created more than a decade ago when computing resources aren't nearly what they are now. Even today, builds can take a considerable amount of time. Semicolons help to clearly demarcate different commands (for the user and for the compiler!) and assist both the programmer and the compiler in understanding what's happening.

Frequency
+28  A: 

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.

Jason Williams
This isn't ambiguous, it's just wrong. You can't multiply an `int` with a pointer, and a terminal can't be an lvalue.
wilhelmtell
There are other examples one could come up with that wouldn't lead to errors. `int x = y * z--`
Dennis Zickefoose
I like this answer more because it seems there is nothing which can be done in syntax to solve this.
SysAdmin
Think that the programmer has made a mistake, and the compiler has noticed. Can the compiler guess that the programmer missed a `;`? What if the programmer missed something else (`*` could fit above)? The compiler knows that you have made a mistake, but it cannot possibly know what you meant to do.
David Rodríguez - dribeas
+1 For illustrating that the compiler can't always determine what you really meant, and in that case there's no point in guessing some of the time.
Mark B
+2  A: 

In C programs semicolons are statement terminators, not separators. You might want to read this fun article.

Bozhidar Batsov
A: 

+1 to you both.

The semi-colon is a command line delimiter, unlike VB, python etc. C and C++ ignore white space within lines of code including carriage returns! This was originally because at inception of C computer monitors could only cope with 80 characters of text and as C++ is based on the C specification it followed suit.

I could post up the question "Why must I keep getting errors about missing \ characters in VB when I try and write code over several lines, surely if VB knows of the problem it can insert it?"

Auto insertion as has already been pointed out could be a nightmare, especially on code that wraps onto a second line.

ChrisBD
+2  A: 

; is for the programmer's convenience. If the line of code is very long then we can press enter and go to second line because we have ; for line separator. It is programming conventions. There must be a line separator.

Himadri
Is that why you think semicolon is a must? read other answers...
SysAdmin
@Sys: He's right. C and C++'s syntax requires a statement terminator of some sort. That's what the other answers are saying.
Dennis Zickefoose
@Dennis Zickefoose - I agree to what you are saying, but I just dont agree that ; is a line seperator and its for programmer's convenience
SysAdmin
I mean statement terminator.. Sorry for using wrong word.
Himadri
A: 

I won't extend much of the need for semi-colon vs line continuation characters, both have advantages and disadvantages and in the end it's a simple language design choice (even though it affects all the users).

I am more worried about the suggestion for the compiler to fix the code.

If you have ever seen a marvelous tool (such as... hum let's pick up a merge tool) and the way it does its automated work, you would be very glad indeed that the compiler did not modify the code. Ultimately if the compiler knew how to fix the code, then it would mean it knew your intent, and thought transmission has not been implemented yet.

As for the warning ? Any programmer worth its salt knows that warnings should be treated as errors (and compilation stopped) so what would be the advantage ?

Matthieu M.
+2  A: 

Having semi-colons (or line breaks, pick one) makes the compiler vastly simpler and error messages more readable.

But contrary to what other people have said, neither form of delimiters (as an absolute) is strictly necessary.

Consider, for example, Haskell, which doesn’t have either. Even the current version of VB allows line breaks in many places inside a statement, as does Python. Neither requires line continuations in many places.

For example, VB now allows the following code:

Dim result = From element in collection
             Where element < threshold
             Select element

No statement delimiters, no line continuations, and yet no ambiguities whatsoever.

Theoretically, this could be driven much further. All ambiguities can be eliminated (again, look at Haskell) by introducing some rules. But again, this makes the parser much more complicated (it has to be context sensitive in a lot of places, e.g. your return example, which cannot be resolved without first knowing the return type of the function). And again, it makes it much harder to output meaningful diagnostics since an erroneous line break could mean any of several things so the compiler cannot know which error the user has made, and not even where the error was made.

Konrad Rudolph
A: 
int sdf = 1,df=2;
sdf=1 df =2

I think the general problem is that without the semicolon there's no telling what the programmer could have actually have meant (e.g may-be the second line was intended as sdf = 1 + df - 2; with serious typos). Something like this might well result from completely arbitrary typos and have any intended meaning, wherefore it might not be such a good idea after all to have the compiler silently "correct" errors.

You may also have noticed that you often get "expected semicolon" where the real problem is not a lack of a semicolon but something completely different instead. Imagine a malformed expression that the compiler could make sense out of by silently going and inserting semicolons.

The semicolon may seem redundant but it is a simple way for the programmer to confirm "yes, that was my intention".

Also, warnings instead of compiler errors are too weak. People compile code with warnings off, ignore warnings they get, and AFAIK the standard never prescribes what the compiler must warn about.

visitor
+1  A: 

Can anybody please explain to me why such questions get many votes?

It is not the job of the compiler to do that. And, as noted before, it is close to impossible to get it right in complex cases. But the bottom line is that it wouldn't even be the responsibility of the compiler if it were perfectly clear what was meant.

Why is it not the responsibility of the compiler? Whose else should it be? The job of the compiler is to understand a programming language and the job of *that* is to make the programmer’s life easier. If that is done by not having semi-colons, then by all means it *is* the compiler’s job to understand that.
Konrad Rudolph
@Konrad: It is the job of the compiler to understand a language according to the definition of this language. It is not the compiler's job to guess the programmer's intend, because it is the programmer's job to write code according to the language definition. If it is not defined in the language definition how to handle missing semicolons, then a conforming compiler MUST treat this as a syntax error. If you want to change that, then change the C/C++ standards.
Secure