Title: Why can you use multiple semicolons in C?
Tags: c
Question: In C I can do the following:

```int main()
{
 printf("HELLO WORLD");;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}
```

and it works! Why is that?

My personal idea: semicolons are a NO OPERATION (from Wikipedia) indicator, having a giant string of them serves the same idea as having one and telling C that a statement has ended.

Comment: The semicolon separates statements. You aren't telling the compiler to do anything between those semicolons, so what shouldn't work about it?

Comment: @Blender: The semicolon isn't really a statement separator in C, it's really a statement terminator.  There exist languages where it's a statement separator.  In those languages, the semicolon after the final statement in a block is optional.

Comment: Writing code that serves no purpose is valid in any programming language. What makes you think C would make an exception?

Comment: @DietrichEpp: not necessarily optional. For example in Pascal (at least as defined by Wirth) a semicolon immediately before an `else` is an error -- the compiler won't accept it.

Here is the accepted answer: A semicolon terminates a statement... consecutive semicolons represent no-operation statements (as you say).  Consider:

```while (x[i++] = y[j++])
    ;
```

Here, all the work is done in the loop test condition, so an empty statement is desirable.  But, empty statements are allowed even when there is no controlling loop.  

Why?

Well, many uses of the preprocessor may expand to some actual C code, or be removed, based on some earlier defines, but given...

``` MY_MACRO1();
 MY_MACRO2();
```

...the preprocessor can only replace the ```MY_MACROX()``` text, leaving the trailing semicolons there, possibly after an empty statement.  If the compiler rejected this it would be much harder to use the preprocessor, or the preprocessor calls would be less like non-preprocessor function calls (they'd have to output semicolons within the substitution, and the caller would have to avoid a trailing semicolon when using them) - which would make it harder for the implementation to seemlessly substitute clever macros for functions for performance, debugging and customisation purposes.


Here is another answer: C allows null statements. They can be useful for things like empty loops:

```while (*d++ = *s++)
   ; // null statement.
```

You've just created a series of them.

It also allows not-quite-null statements like:

```0;
1+1;
```

Both of these contain expressions, but with no side-effects, so they don't really do anything. They're allowed, though a compiler might warn about them.

A decent compiler won't normally generate any code for any of the above (most won't even with optimization turned off, and I can't imagine one that would with optimization turned on).


Here is another answer: Semicolons are line terminators, meaning they tell that the codes have reached the end of the line, THEN, do the next line of codes.

One proof is that you can write your codes in a single line, excluding the directives.

```main() { cout << "ENTER TWO NUMBERS"; cin &gt;&gt; a; cin &gt;&gt; b; cout << "The sum of two numbers are" << a+b; << return 0;}
```

Which could mean
    main() { cout << "ENTER TWO NUMBERS"[THEN] cin >> a[THEN] cin >> b[THEN] cout << "The sum of two numbers are" << a+b[THEN] << return 0[THEN]}

so if you were to place multiple semicolons, it's like, a THEN, THEN, THEN, THEN, and your personal idea is correct indeed.


Here is another answer: Because a semicolon identify the end of a statement in C and in your case more semicolons identifies more empty statements ... There is nothing wrong, they are just empty statements .


Here is another answer: Two semicolons together make an empty statement. C doesn't mind having empty statements - they don't generate any code.

Comment for this answer: @JerryCoffin even in the for case they are empty statements, they are empty statements that evaluate to true ...
Comment for this answer: Or eventually they generate `nop` instruction
Comment for this answer: For the pedants: yes, you're undoubtedly correct: `for(;;)` contains two semicolons together, but not an empty statement.
Comment for this answer: @aleroot: The syntax for a `for` statement is: `for ( clause-1 ; expression-2 ; expression-3 ) statement`. As you can see, what's controlled is a statement, but the others are only expressions, not statements (well, clause-1 can be either an expression or a declaration, but it's still not a statement).
Comment for this answer: @JoSo: By the current standard, yes. They've changed the syntax specification a bit though, so now it's `for ( init-statement condition ; expression )`, and an `init-statement` is explicitly a statement. I'd have to dig up an old draft to be sure, but I *think* at that time, the first clause in a `for` loop was a unique thing that wasn't traceable back to an actual statement (either that or my memory is just bad--all too likely at my age).
Comment for this answer: @JerryCoffin: Technically, a declaration is a statement as well, no?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 5, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:06/04/12', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<c>', 'Title': 'Why can you use multiple semicolons in C?', 'ViewCount': 9798, 'id': 10876776, 'user_AboutMe': "<p>Name intended to be a vocal exercise :)</p>\n\n<p>email: i'll put a tor redirect eventually</p>\n", 'user_AccountId': 976189.0, 'user_DisplayName': 'Eiyrioü von Kauyf', 'user_Reputation': 4235.0, 'user_Views': 655.0, 'user_id': 997301.0}}