do {...} while (0) (was: Uninitialized static int counters? )

Michael ODonnell michael.odonnell at comcast.net
Fri Feb 6 16:53:16 EST 2009



> What I see confufsing is:
>    do { ... } while(0);
> What this means is to go through the loop once.  You need a leading
> curly so you can set up counter as a local variable as variable
> names are block scope.  { ...  } would be equivalent to above.

They're definitely not equivalent - that's why you see do-while(0) so
often.  Try this (admittedly stupid) little program to demonstrate:

   #define b0rken(x) {return x;}
   #define works(x)  do {return x;} while(0)

   int main( int argc, char **argv )
   {
       if( argc > 1 )
           b0rken( argc ); /* Try works() here, instead */
       else
           return( 42 );
   }
 


More information about the gnhlug-discuss mailing list