I've been using Groovy, and it seems to be able to handle similar C/C++ syntax without semicolons. My question is, why does C/C++ require semicolons? Is it "that's just how they designed it", or are there ambiguities in the language that are preventable by using semicolons? If the latter, please give examples of such ambiguities.
Asked
Active
Viewed 135 times
-3
-
3A nice related question: [Why doesn't the compiler report a missing semicolon?](http://stackoverflow.com/questions/40135392/why-doesnt-the-compiler-report-a-missing-semicolon) – David Ranieri Nov 12 '16 at 13:56
-
2Because it doesn't use newlines to separate statements. – glauxosdever Nov 12 '16 at 14:01
-
@glauxosdever Other languages are able to differentiate between statement terminators, and those made for style. I'm under the impression that statement terminators wouldn't be required at all. – Chris Smith Nov 12 '16 at 14:06
-
Such languages however require some special character sequences. For example the shell language requires \. Same with the C preprocessor. – glauxosdever Nov 12 '16 at 14:22
1 Answers
2
There's no "reason" as such. That is how the language constructs were written. The syntax includes the usage of ;
as the authors mentioned.
Just to quote a related paragraph from C11
, chapter §5.1.1.2, Translation phases
- White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
and, newline is a whitespace.

Sourav Ghosh
- 133,132
- 16
- 183
- 261
-
-
@ChrisSmith: because of the semicolons, you can safely ignore all whitespace - including end-of-lines. `int main(void) { return 0; }` is valid. – Jongware Nov 12 '16 at 13:55
-
2@ChrisSmith As c and c++ do not treat `\n` as a statement delimiter, there would be many ambiguities, yes. – πάντα ῥεῖ Nov 12 '16 at 13:56
-
@chris: if you just remove the semicolons, there would be lots of ambiguities. I can't know which ones you might be aware of. – rici Nov 12 '16 at 13:58
-
@rici I'm only aware of the one posted in the main question comments. Other than that, I don't know of any. – Chris Smith Nov 12 '16 at 14:00
-
1@chris: Here's another one, then: `if(c)return a=42` also, consider a function declaration followed by a block statement. The largest category is the one referenced by that comment, though: two expressions which combined, form a single one. – rici Nov 12 '16 at 14:30