-4

Why does the C/C++ parser not report an error on a semicolon when it is used alone? For example:

int a;
;
int b;

Is it a consequence of language grammar rules or is it a consequence of parsing algorithms? What do standards say about such a case? Whether ; or e.g. int; should be allowed?

Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
igntec
  • 1,006
  • 10
  • 24

3 Answers3

2

Consider the classic string copy function from K&R:

void strcpy(char *src, char *dst)
{
    while (*src++ = *dst++);
}

As you can see the empty statement is not just legal, it's one of foundations of the short C syntax.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
1

A semicolon terminates a statement.

If your statement doesn't do anything, that's your prerogative as a programmer.

An example of a completely valid use of an empty statment:

 for (int x = 0;; x++) {
     if (something(x)) {
         return 5;
     }
     if (bah(x)) {
         continue;
     }
     if (otherthing(x)) {
         return 3;
     }
 }

The middle statement in the for loop is empty. But it is still necessary, as the for loop takes three distinct statements, and the first and third are populated.

I don't see a good reason why a lone semicolon should be invalid; it does no harm. It is allowed by all relevant C/C++ standards, which is not a worse position than it not being allowed.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 1. **Is it** allowed by all standards? (even pre-C++11?) IIRC it's not. – Luchian Grigore Sep 07 '15 at 11:05
  • @LuchianGrigore At the top of the loop. `for (int x = 0;; x++)`. Note the double semicolon. – Borealid Sep 07 '15 at 11:09
  • 1
    @LuchianGrigore - the empty statement is in `for (int x = 0;; x++)` - note the two semi-colons consecutively, this is an empty statement. As far as I know, the example given in the answer has been valid in all C and C++ standards, as well as K&R. – Tom Sep 07 '15 at 11:09
  • @LuchianGrigore If it not allowed by C98, then I've never seen a conforming compiler in my entire life. I have yet to have a program rejected because it contains an extra semicolon at the end of a line. – Borealid Sep 07 '15 at 11:10
  • Ah, missed the loop condition, my bad. – Luchian Grigore Sep 07 '15 at 11:15
  • empty statement within for loop is legal even in K&R. As a side note, inline `int` declaration is not. – Non-maskable Interrupt Sep 07 '15 at 11:26
  • 2
    I hate to say it, but your for-loop does not contain an empty (null) statement. It merely contains a missing condition. To see it, consider that you cannot put statements there (but only conditions which are expressions or declarations whose type can convert to booleans). What is similar is merely that both in the null statement and the missing condition, an expression that is optional is missing, but that's about it. – Johannes Schaub - litb Sep 07 '15 at 11:29
  • @JohannesSchaub-litb Perhaps I should have left the first one blank, then. That doesn't have to be a boolean... – Borealid Sep 07 '15 at 11:32
  • litb's right; this example is wrong. – Lightness Races in Orbit Sep 07 '15 at 12:53
  • for (int x = 0; true; x++) {} is more readable imho – Ben Alan Dec 06 '21 at 23:15
0

It's specified in the standard, §6/1 and §6.2/1 (revision N3376):

statement:

...

attribute-specifier-seq_opt expression-statement

...

and

expression-statement:

expression_opt;

So a statement can be an expression statement with a colon and optionally a expression and an attribute specifier sequence before.

Community
  • 1
  • 1
JBL
  • 12,588
  • 4
  • 53
  • 84