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.
Re:What is your favourite C programming trick?
by mikeash on Friday October 14, 2011 @11:32AM
(#3111550)
Would be nice if there was one which would warn for this specific case but not shadowing in general, which I occasionally like. (This probably makes me a bad person.)
Re:What is your favourite C programming trick?
by ajross on Friday October 14, 2011 @12:11PM
(#3111723)
It certainly doesn't make you a bad person. If lexical scoping wasn't the intent of the language authors, it wouldn't exist. I tend to agree that the warning is more of a hassle than it's worth. Though I guess it can be argued that if your scoping is so deep that you actually need to reuse names, that perhaps some refactoring should be in order.
Re:What is your favourite C programming trick?
by kstenerud on Friday October 14, 2011 @12:10PM
(#3111714)
Seeing a variable shadow something in an outer scope makes me cringe, simply because the decrease in readability far outweighs any benefit it could give. You might remember how things are, but the poor sap who has to keep extra scoping depths in his mind just to maintain your code will curse your name every day.
See: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html