'Variable initialization in for loop
I had such a question:What is the difference between
int i;
for(i = 0; i < n; i++)
and
for(int i = 0; i < n; i++)
Does this influence memory and time?
Solution 1:[1]
Assuming you have written this code in the main function,
int i;
for(i = 0; i < n; i++)
In the case above, i is a local variable of the main function. If the value of i is updated in the loop it will remain updated even after the loop ends. i is not destroyed after the for loop. This is useful in some programs where you need the value of i after the loop for later use.
for(int i = 0; i < n; i++)
In the case above, i is a local variable of the for loop. i will be destroyed once the loop is over.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Dobariya |
