Forums | MacLife
You are not logged in.
#1 2008-03-01 12:51 am
C99 Mode?
Okay, so trying to finish up my lab and I ran into an error...
'for' loop declaration used outside C99 mode
So, I googled it and I think I get what it means but I don't see anywhere to change the language settings to get C99 mode. Anybody want to tell me how to change this?
Thanks!
Offline
#2 2008-03-01 12:57 am
Re: C99 Mode?
Are you doing something like for (int i = 0; ...; ...)? If so, you need to declare i outside the for statement.
Offline
#4 2008-03-01 12:26 pm
Re: C99 Mode?
loaf wrote:
So I need to either change the for loops to while loops or not write for loops how I've learned them, I guess?
Excerpted from the GCC man page:
Options Controlling C Dialect
The following options control the dialect of C (or languages derived
from C, such as C++, Objective-C and Objective-C++) that the compiler
accepts:
-std=
Determine the language standard. This option is currently only
supported when compiling C or C++. A value for this option must be
provided; possible values are
c99
c9x
iso9899:1999
iso9899:199x
ISO C99. Note that this standard is not yet fully supported;
see <http://gcc.gnu.org/gcc-4.0/c99status.html> for more
information. The names c9x and iso9899:199x are deprecated.
gnu89
Default, ISO C90 plus GNU extensions (including some C99
features).
gnu99
gnu9x
ISO C99 plus GNU extensions. When ISO C99 is fully implemented
in GCC, this will become the default. The name gnu9x is
deprecated.
So you need to either:
- Add -std=c99 or -std=gnu99 to your compilation options
- Write your for loop like:
Code:
int i; for (i = 0; ...)
Offline
#5 2008-03-01 1:40 pm
Re: C99 Mode?
loaf wrote:
Yep.
So I need to either change the for loops to while loops or not write for loops how I've learned them, I guess?
What Miles said. Though I suggest doing
Code:
int i; for (i = 0; ...; ...)
because the Microsoft compiler doesn't support C99, so it's more portable. Note, however, that C++ allows you to do for (int i = 0; ...; ...), since it's much more lenient on where you can declare variables. With standard C you can only declare variables at the beginning of a code block, but in C++ (and I believe C99, and somewhat with GCC with its default settings) you can declare variables almost anywhere in code. Note that this also means that if you declare variables in the middle of code in a C source file, (aka: not at the beginning of the block, and putting it after an assignment, function call, etc.) it might compile under GCC under the default settings, but not under the Microsoft compiler...
Offline
#6 2008-03-01 2:32 pm
Re: C99 Mode?
loaf wrote:
Yep.
So I need to either change the for loops to while loops or not write for loops how I've learned them, I guess?
You would still have to pre-declare any variable you used with a while loop. C++ added the ability to do spontaneous variable declaration (although I believe the compiler still declares the variables beforehand, it just parses your code with more sophistication).
...having nothing in them of the feelings or principles of '76, now look to a single and splendid government of an aristocracy, founded on banking institutions and moneyed incorporations under the guise and cloak of their favored branches of manufactures, commerce and navigation, riding and ruling over the plundered ploughman and beggared yeomanry. -- TJ
Offline
#7 2008-03-01 11:25 pm
Re: C99 Mode?
In older versions of C++, doing for (int i = 0; ...; ...) would have i be visible in the outer scope. In newer versions, however, it isn't the case. (it will give you an error if you try using it outside the loop, saying that it isn't supported in the newer standard)
Offline
#8 2008-03-02 1:56 pm
Re: C99 Mode?
akb825 wrote:
because the Microsoft compiler doesn't support C99, so it's more portable.
Well, in class we're using Microsoft Visual Studio, so I guess I just need to avoid for loops as I've been taught them then. Because if I use C99 at home, the code won't compile at school.
Offline
#9 2008-03-02 8:28 pm
- Booksley
- Planely insane!
- From: Toronto, Ontario
- Registered: 2001-02-16
- Posts: 4826
Re: C99 Mode?
loaf wrote:
akb825 wrote:
because the Microsoft compiler doesn't support C99, so it's more portable.
Well, in class we're using Microsoft Visual Studio, so I guess I just need to avoid for loops as I've been taught them then. Because if I use C99 at home, the code won't compile at school.
I think you need to get Visual Studio at home, if you're planning on doing any work at home. If you've got an Intel Mac, give Boot Camp a go. If not, I'd suggest setting aside some extra time in the computer lab at school, because the differences between GCC and Visual Studio will drive you insane.
Last edited by Booksley (2008-03-02 8:28 pm)
Offline
#10 2008-03-02 9:21 pm
Re: C99 Mode?
If you use the -pedantic flag on GCC, it will complain more but your code will be more "standard". Code that compiles under GCC with the -pedantic flag should compile under Visual Studio. (that is, barring the differences in the standard library) You can enable this in XCode in the build settings for your project. Note that these will be issued as warnings rather than errors. (since it can obviously still compile the code, it's just telling you it isn't standard) Regardless, it's good practice to always fix any warnings you have. You should definitely still compile it under Visual Studio, but this should help alleviate some of the headache of making it work.
Offline
#11 2008-03-04 11:41 pm
Re: C99 Mode?
Couldn't you just compile your programs as C++ instead of C? C++ is where this syntax is always definitely legal.
...having nothing in them of the feelings or principles of '76, now look to a single and splendid government of an aristocracy, founded on banking institutions and moneyed incorporations under the guise and cloak of their favored branches of manufactures, commerce and navigation, riding and ruling over the plundered ploughman and beggared yeomanry. -- TJ
Offline
