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.
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.
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.
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.
There is nothing inherently ungrammatical about using multiple semicolons to connect three or more closely related independent clauses.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With