Hi.
Here's a quote from C++ Primer - Fifth Edition:
While I can see why that kind of practice is beneficial (defining variables as late as possible, besides what the book pointed out,Advice: Define Variables Where You First Use Them
It is usually a good idea to define an object near the point at which the
object is first used. Doing so improves readability by making it easy to find
the definition of the variable. More importantly, it is often easier to give the
variable a useful initial value when the variable is defined close to where it is
first used.
also minimizes their scope, and consequently reducing bugs potential - at least that's my take on it),
I don't understand why in C, in a course I took a couple of years ago, I was encouraged to do the opposite - "define any variable you'll use in advance,
at the head of the function body".
Is there some performance advantage in C when defining variables in advance, that doesn't exist in C++?
Or maybe I was provided with misinformation in that course?