
Hi Reyann,
Good question. If youâre seeing both double i; and int i in the same code, hereâs whatâs happening:
The double i is declared outside the for loop, and the int i is declared inside the loop. In C, variables declared inside a block (like a loop) are separate from those outside â even if they have the same name.
So when you write:
double i = 3.5;
for (int i = 0; i < 5; i++) {
// this 'i' is a different variable
}
The int i inside the loop is its own separate variable, and it temporarily hides the double i. Thatâs why the compiler doesnât complain â itâs allowed, but it can be confusing.
Best practice: avoid using the same variable name in different scopes unless thereâs a good reason.
If you have more questions, I am here to help.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
