質問

The loop is as follows:

do;
while(1);

Why the above loop is not giving a syntax error?

I am using mingw gcc compiler

役に立ちましたか?

解決

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.

他のヒント

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top