27

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SysAdmin
  • 5,455
  • 8
  • 33
  • 34

9 Answers9

55

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
  • 79,602
  • 28
  • 170
  • 210
  • 10
    +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 May 05 '10 at 07:32
  • 1
    The Javascript example is the first one that came to my mind as well. – Steve Rowe May 05 '10 at 07:34
  • So why not add a line continuation char like _ as it is in other languages – SysAdmin May 05 '10 at 07:38
  • 5
    @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. – JoeG May 05 '10 at 07:42
  • @SysAdmin Because C++ is not those other languages. It has its very well defined semantics that people can rely on. – Daniel Daranas May 05 '10 at 07:44
  • Why should? Thats the same as asking why should there be a semi-colon delimiter in C and C++. – ChrisBD May 05 '10 at 07:44
  • @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 May 05 '10 at 07:46
  • 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 May 05 '10 at 08:04
  • @Sys: you can omit the return value for `main`. – Dennis Zickefoose May 05 '10 at 08:10
  • @Dennis Zickefoose - if you put a return statement in main, you will get an error – SysAdmin May 05 '10 at 08:15
  • @Sys: I double checked, and you are correct, my apologies. Not returning is the same as returning 0, but `return` is not. – Dennis Zickefoose May 05 '10 at 08:21
  • @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. – JoeG May 05 '10 at 08:25
  • @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 May 05 '10 at 08:30
  • C++ does have a line continuation character: \ – graham.reeds May 05 '10 at 08:36
  • This is the answer that made me understand why to add semicolon. – Lok Jun Jan 06 '14 at 09:26
30

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
  • 56,972
  • 11
  • 108
  • 137
  • 5
    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 May 05 '10 at 07:40
  • 1
    There are other examples one could come up with that wouldn't lead to errors. `int x = y * z--` – Dennis Zickefoose May 05 '10 at 07:44
  • I like this answer more because it seems there is nothing which can be done in syntax to solve this. – SysAdmin May 05 '10 at 07:49
  • 4
    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 May 05 '10 at 07:54
  • +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 May 05 '10 at 14:54
8

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.

Pete Schlette
  • 1,948
  • 1
  • 13
  • 18
4

; 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
  • 2,922
  • 5
  • 32
  • 56
  • @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 May 05 '10 at 08:25
  • @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 May 05 '10 at 08:33
3

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
  • 530,221
  • 131
  • 937
  • 1,214
2

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

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
0

+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
  • 9,104
  • 3
  • 22
  • 35
0

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.
  • 287,565
  • 48
  • 449
  • 722
0
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
  • 8,564
  • 2
  • 26
  • 15