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 semicolon terminates a statement… consecutive semicolons represent no-operation statements (as you say). Consider:
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…
…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.