Suppose we have something along these lines
int f(int n);
….
do{
int a = b;
int b = f(a);
}
Is there any risk to saying
do{
int b = f(b);
}
instead, assuming a is not used elsewhere? Would it be stylistically preferable to do the former?
It is important to understand that you’re not “altering” any variables here, just changing the meaning of a name.
The important concept is called the point of declaration.
Let’s look at your example:
or
The name b references two objects, depending on where you are in the code, let’s call them b1 and b2. This is unsurprising in case 1:
Since in C the point of declaration is before the initializer, the second example resolves differently:
Note that here there’s no link between b1 and b2, you’re initializing b2 with its own (undefined) value. A diligent compiler will warn you about this, like