Error msg: 'for' loop initial declaration used outside C99 mode

I usually write "for" loops like:

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

which works fine in the main sketch. However, if I move this code to a seperate file, say functions.c and then include that in my main sketch, I get the error:

'for' loop initial declaration used outside C99 mode

I can fix this by doing:

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

but I'd like to keep the original definition because it scopes the "i" to within the loop.

Google research says that I need to compile in C99 mode - how can I do this for ALL files? I'm confused why the compiler is presumably compiling the main sketch in this mode, but not any included files?

You should put other code in a .h, not a .c file and #include it. Either that or write a library using .h and .cpp files.
Currently you are compiling your code as C and not C++, hence the error.

Or make it a .ino file, or .cpp
.c is especially magic and gets old style c compiler, while .cpp will get the c++ compiler, which will do most C just fine.

ah, changing the .c to .cpp has fixed it thanks!

MarkT:
You should put other code in a .h, not a .c file and #include it.

Queue the sound of a million comp-sci instructors screaming. :wink: