2 ms·
> You will just have to remember to declare all of your variables at the top of each function This is still idiomatic to the C community, even if it isn't stri
by deaddodo 3y ago
> You will just have to remember to declare all of your variables at the top of each function
This is still idiomatic to the C community, even if it isn't strictly required any longer. About the only place it's ignored is inline variables for loops.
- vkoskiv 3y agoI 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.