Da Mike
Da Mike

Reputation: 463

Local counter variable in for loop

Is this acceptable by all C Standards?

for (int i=0; i<n; i++) {
    // do stuff
}

Or should I write it like this just to be sure it works everywhere?

int i;
for (i=0; i<n; i++) {
    // do stuff
}

Upvotes: 0

Views: 1041

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

No, it's valid since C99 only. If you want your code to be valid under old standards use

int i;
for (i = 0 ; i < n ; i++)

And also, read this comment by @JoachimPileborg it complements well this answer.

Upvotes: 2

Related Questions