Originally Posted by
Tomwa
On the concept of initialization, initializing a variable to an incorrect value is a typographical mistake or a logical one that can be debugged and the behavior will be the same each time
If there is no correct initial value at that point, then there is no correct initialisation (or any initialisation, or no initialisation, is correct). The appropriate solution is to declare the variable near to first use, or in such cases, near to where they will be provided with an appropriate initial value. Just initialising the variable for the sake of initialisation will not cut it, and in fact can mislead the reader into thinking that the initialisation is appropriate.
EDIT:
Originally Posted by
Tomwa
it'd be like if i did
Consider this code:
Code:
int i = 0;
for (i = 2; i < 10; ++i)
{
printf("%d\n", i);
}
Is the assignment of 2 to i a bug or was it intentional? If it was intentional, why was i initialised to 0 instead of 2? Now consider this code:
Code:
int i = 0;
scanf("%d", &i);
printf("%d\n", i);
When run with certain user input, the program prints "2". Was the initialisation of i with 0 a bug or was it intentional? If it was intentional, why was i not used before the scanf?