News for hackers. Stuff that matters.
Re:What is your favourite C programming trick? by silentbicycle on Friday October 14, 2011 @10:02AM (#3111185)
Definitely. Know your warning options! Also, try using multiple compilers. tcc compiles very quickly, clang often has better error messages, etc.

Speaking of variable shadowing: It's usually worth wrapping any preprocessor macros in a "do { ... } while (0)" block unless you deliberately want variable definitions to escape (in which case, token pasting a suffix is usually a good idea).

Re:What is your favourite C programming trick? by AlexandrB on Friday October 14, 2011 @01:44PM (#3112135)
Isn't "do { ... } while (0)" the same as just "{ ... }"?

I sometimes use "{ ... }" to limit the scope of a variable that is only needed for a small section of code.

Re:What is your favourite C programming trick? by huhtenberg on Friday October 14, 2011 @05:14PM (#3112981)
Former lets break'ing out, latter doesn't.
Re:What is your favourite C programming trick? by enqk on Friday October 14, 2011 @02:22PM (#3112300)
do { ... } while (0) makes your macro behave like a statement (i.e. the semicolon is mandatory)
Re:What is your favourite C programming trick? by silentbicycle on Friday October 14, 2011 @02:48PM (#3112409)
Yes. I don't include the trailing semicolon in macro definitions, because I expect the macro to be followed by them:

    MACRO(foo);
Re:What is your favourite C programming trick? by rcfox on Friday October 14, 2011 @02:42PM (#3112380)
({ ... }) does this too. (Is this GCC-specific?)
Re:What is your favourite C programming trick? by mansr on Friday October 14, 2011 @02:56PM (#3112433)
That is a gcc extension. The construct as a whole has the value of the last statement executed within. Usually an inline function is preferable since it achieves the same thing using only standard syntax.