3 ms·
The convention of declaring all the variables at the top originates in Fortran, so it is indeed very old school. When writing a program, I find it more conveni
by adrian_b 5y ago
The convention of declaring all the variables at the top originates in Fortran, so it is indeed very old school.
When writing a program, I find it more convenient to declare the variables at the first point of use.
Nevertheless, when reading a program written by someone else, I strongly prefer to see all the variable declarations together.
It might be that this preference is caused by the fact that I mainly use C when writing programs for resource-constrained embedded environments and in such applications it is valuable to be able to estimate at the first glance to a function which are its memory requirements.
- jes 5y agoI don’t write in C very often anymore. But when I last did, I found myself establishing local scopes inside functions so that variables I introduced went out of scope when I was done with them. I’m also an oddball, so there’s that.
- Someone 5y agoWith non-trivial functions and modern compilers, that gives you an upper bound that may differ significantly from the actual stack usage. There is no requirement for the compiler to keep the memory of all locals distinct. On more powerful CPUs, chances also are a large fraction of the locals never makes it onto the stack, but stays in registers. Having said that, compilers for embedded CPUs used to be fairly bad at optimizations (wouldn’t know of that has changed in the past decades)