Nunyah
April 22nd, 2007, 03:50 PM
Hello, I've recently started out with the C language and I'm currently learning it from http://beej.us/guide/bgc/ and I got a question with a error message produced by GCC on my Ubuntu 7.04 box.
Consider this code from a tutorial at it's site:
#include <stdio.h>
main()
{
int num = 10;
for(int i=0;i<num;i++) {
printf("Number is %d.\n", i);
}
}
Results in the following error message when compiling with GCC:
error.c: In function main:
error.c:7: error: for loop initial declaration used outside C99 mode
If i declare the i variable before the for loop, it will compile as it should:
#include <stdio.h>
main()
{
int num = 10;
int i;
for(i=0;i<num;i++) {
printf("Number is %d.\n", i);
}
}
Is it something wrong with my GCC settings since the first one won't work?
Consider this code from a tutorial at it's site:
#include <stdio.h>
main()
{
int num = 10;
for(int i=0;i<num;i++) {
printf("Number is %d.\n", i);
}
}
Results in the following error message when compiling with GCC:
error.c: In function main:
error.c:7: error: for loop initial declaration used outside C99 mode
If i declare the i variable before the for loop, it will compile as it should:
#include <stdio.h>
main()
{
int num = 10;
int i;
for(i=0;i<num;i++) {
printf("Number is %d.\n", i);
}
}
Is it something wrong with my GCC settings since the first one won't work?