Domanda

The loop is as follows:

do;
while(1);

Why the above loop is not giving a syntax error?

I am using mingw gcc compiler

È stato utile?

Soluzione

That code looks like it was written to obfuscate its meaning but if we look at the draft C99 standard section 6.8.5 Iteration statements, the grammar for do while is:

do statement while ( expression ) ;

and statement can be an expression statement which in turn can be a null statement which is what ; is. So it is the same as:

do
  ;
while(1);

we can see from section 6.8.3 Expression and null statements paragraph 3 says:

A null statement (consisting of just a semicolon) performs no operations.

Altri suggerimenti

do
    ; //nop
while(1);

Now it is more readable :)


Note that the ; is a Null Statement.

This is a single statement do-while loop equivalent to

do
    no_op(); // do nothing
while (1);

or

do {
}
while (1);

Personally, I hate null statements and I'd be perfectly happy to have them removed from the standard.

My preferred form would be

do continue;
while (1);

Because it is an infinite loop with an empty statement ;.

do
{
   ;

}while(1);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top