![]() |
which one is "standard"...
Code:
for (int i = 0; i < 5; i++) { Code:
int i; Code:
{ |
Re: which one is "standard"...
the first one may make problems, the second one is quite clear and ok, and the third one is just for those ppl not able to use a variable twice, and therefore need different scopes for each ;)
I'd prefer the second one |
Re: which one is "standard"...
s/"...so which one is the 'standard' one ?"/"...so which one is the 'standard' one, MSVC or GNU ?"
:) |
Re: which one is "standard"...
what do you mean by standard??
as long as MSVC and GCC can interpret it correct it'll work just fine :D |
Re: which one is "standard"...
It depends on what YOU want the scope of the variable 'i' to be. If you only want it to be valid inside the 'for' loop then the first one is "standard" (according to ANSI standards for C++).
If you want to know what the value of 'i' was below the 'for' loop you'd need to use the second case. For example, you could have something like this... Code:
int i; NOTE: Microsoft Visual C++ 6.0 doesn't adhere to the same ANSI standards as gcc. So, for example, this code is perfectly valid in MSVC 6.0, but not in gcc... Code:
for (int i=0; i < 10; i++) botman |
Re: which one is "standard"...
In MinGW, you cannot declare an int inside of a loop. It says something about C99 mode. You would have to have it like this:
Code:
int i; |
Re: which one is "standard"...
C99 is the latest ANSI C specification. Former ones are known as 'Traditional C' (thanks again Austin for your C reference manual!!!). MinGW is right, it's MSVC which is wrong here ;)
|
Re: which one is "standard"...
its odd though, i found a pretty old book which is about C and C++, but they always use the example with INT within the 'for' line. So its not even ANSI compliant, which is odd for a book claiming it is... stupid books! :D
|
Re: which one is "standard"...
I think its changed later because in the c++ book off Stroustrup it will even say its valid...
|
Re: which one is "standard"...
Well, C99 came out in 1999, which I believe is after the release of MSVC++. It is probably newer than your book, too.
|
All times are GMT +2. The time now is 07:16. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.