Skip to contentSkip to conversation

C99 doesn't allow local scope variables?

DISCUSSION tabris 3 participants 3 messages Jan 27, 2004

lore
Share & tools
lore

tabris

author
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

simple C program

#include <stdlib.h>
#include <stdio.h>

int main(void)

{
    for (int i=0; i < 2; i++)
        printf("crap\n");
};

[tabris@tabriel tmp]$ gcc test.c -o test
test.c: In function `main':
test.c:6: error: `for' loop initial declaration used outside C99 mode

What does that mean, and why am I not allowed to make a local scope variable for a for{;;} loop?

works fine if i declare the variable outside the loop. But I swear I used to do this with gcc 2.95.
- --
tabris
- -
Body by Nautilus, Brain by Mattel.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAFc1J1U5ZaPMbKQcRAiJyAJ4namXjJa9zd6Y/VGbR+Y5pyHVjUwCcCy4s dW251Zxh2o3I97eabiisOMg=
=21yl
-----END PGP SIGNATURE-----

#1 lore raw

Hello tabris,

On Mon, 26 Jan 2004 21:30:30 -0500 tabris <[email protected]> wrote:

[snip]

Quoted 11 lines

[tabris@tabriel tmp]$ gcc test.c -o test
test.c: In function `main':
test.c:6: error: `for' loop initial declaration used outside C99 mode

What does that mean, and why am I not allowed to make a local scope variable

for a for{;;} loop?

works fine if i declare the variable outside the loop. But I swear I used to

do this with gcc 2.95.

ANSI C89 requires int i; to be declared outside from the for(), only ANSI C99 and C++ allow this, IIRC.

Regards,

Signature · 1 line
wwp
#2 lore raw
In reply to tabris
Quoted 6 lines · tabris

simple C program

#include <stdlib.h>
#include <stdio.h>

int main(void)

Quoted 8 lines
{
    for (int i=0; i < 2; i++)
        printf("crap\n");
};

[tabris@tabriel tmp]$ gcc test.c -o test
test.c: In function `main':
test.c:6: error: `for' loop initial declaration used outside C99 mode

You are trying to use C99 syntax in a file which isn't being treated as such.

The default C dialect is C89 plus GNU extensions (-std=gnu89). The Info file for gcc 3.0 says that C99 plus GNU extensions (-std=gnu99) will become the default once the C99 support is complete. Until then, if you want to use C99, you have to explicitly ask for it with -std=c99 or -std=gnu99.

See the "C Dialect Options" node in the gcc Info file for more details.

Quoted 5 lines

What does that mean, and why am I not allowed to make a local scope variable for a for{;;} loop?

works fine if i declare the variable outside the loop. But I swear I used to do this with gcc 2.95.

You may have been using a modified version of gcc, although I suppose that it's possible that -std=gnu99 may have been the default for some versions.

Signature · 1 line
Glynn Clements <[email protected]>