On 02/06/2009 04:53 PM, Michael ODonnell wrote:
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 );
   }
This is a totally different thing. In your line:

b0rken( argc ); The expansion is
{return arc;};
Note the closing ';'.
The reason the works macro compiles is that while(0) requires a semicolon. In other words, in b0rken, is syntactically incorrect as a macro.
But if you change the macro to:

#define b0rken(x) ({return x;}) you will find that the compiler likes it again. it has nothing to do with { ... } vs. do { ... ] while(0); In your example it is about the correct compiler syntax. Basically { .. } defines a block.
Do statement while(expression)
is a statement, so do { ... } while(0) must be terminated by a semicolon because the semicolon terminates a statement. As I stated before, do { ... while(0) generates code to handle the loop. Because the while(0) contains a constant, most compilers can figure that out at parse time.

--
Jerry Feldman <g...@blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
gnhlug-discuss mailing list
gnhlug-discuss@mail.gnhlug.org
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/

Reply via email to