Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can you use multiple semicolons in C?

Tags:

c

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.

like image 706
Eiyrioü von Kauyf Avatar asked Jun 04 '12 05:06

Eiyrioü von Kauyf


People also ask

What does two semicolons mean in C?

A "double semicolon" does not have any special meaning in c. The second semicolon simply terminates an empty statement. So you can simply remove it.

Can you use multiple semicolons?

As a general rule, you may not want to use more than one semicolon in any paragraph. Avoid using two semicolons to connect three sentences. Insert any semicolons or commas needed in the following sentences.

Why do we use semicolons in C?

The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code. They are not used in between the control flow statements but are used in separating the conditions in looping.

Can you use multiple semicolons in a row?

There is nothing inherently ungrammatical about using multiple semicolons to connect three or more closely related independent clauses.


2 Answers

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.

like image 194
Tony Delroy Avatar answered Oct 06 '22 18:10

Tony Delroy


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).

like image 21
Jerry Coffin Avatar answered Oct 06 '22 18:10

Jerry Coffin