4 ms·
I write C primarily for hobby projects, and I only started with C ~10 years ago, so I'm likely in the minority that considers this a code smell for new C code.
by vkoskiv 3y ago
I write C primarily for hobby projects, and I only started with C ~10 years ago, so I'm likely in the minority that considers this a code smell for new C code. I don't want to needlessly extend the scope of my local variables!
Different story of course if you're targeting C89 or older, which I'm actually doing in my current project. I'm writing software on and for my old 68k Macintosh.
- deaddodo 3y ago<< Edited out for being unnecessarily combative >>
- jandrese 3y agoWe might be saying the same thing here, but modern C scopes variables down to the block. So if you have a variable declared for instance inside of a for loop or if statement, that variable is out of scope once you exit the block. This will fail to compile with an "undeclared identifier" error: if ( x > 0 ) { int y = 1; } printf("%d", y); It is a feature that was long overdue in C IMHO.