PDA

View Full Version : GCC compiler problems



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?

duff
April 22nd, 2007, 04:15 PM
Like the warning says, that's a C99 extension. Compiling with "-std=c99" should get rid of it.

Nunyah
April 22nd, 2007, 04:38 PM
gcc error.c -o -std=c99 error
gcc: error: No such file or directory
error.c: In function āmainā:
error.c:6: error: āforā loop initial declaration used outside C99 mode


Can I save std=c99 so I won't have to pass it as a parameter all the time?

DoktorSeven
April 22nd, 2007, 05:47 PM
gcc -std=c99 -o error error.c

And no, you'll have to declare std=c99 every time.

Nunyah
April 22nd, 2007, 08:45 PM
Thank you both.