.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   which one is "standard"... (http://forums.bots-united.com/showthread.php?t=1678)

Whistler 16-05-2004 05:16

which one is "standard"...
 
Code:

for (int i = 0; i < 5; i++) {
  ...
}

looks like in MSVC and Borland compiler 5.4 this is same as:
Code:

int i;
for (i = 0; i < 5; i++) {
  ...
}

but in GNU compiler and Visual Studio.NET beta version this is:
Code:

{
  int i;
  for (i = 0; i < 5; i++) {
      ...
  }
}

...so which one is the 'standard' one ?

@$3.1415rin 16-05-2004 10:18

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

Whistler 16-05-2004 10:39

Re: which one is "standard"...
 
s/"...so which one is the 'standard' one ?"/"...so which one is the 'standard' one, MSVC or GNU ?"
:)

sPlOrYgOn 16-05-2004 14:49

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

botman 16-05-2004 15:15

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;
for (i = 0; i < 10; i++)
{
  if (i == x)
          break;
  printf("i is %d\n", i);
}
if (i == 10) // did the loop finish or break out early?
  printf("the loop executed 10 times\n");

The third example you should would be if you wanted to create a local scope for 'i' that included the 'for' loop and some amount of code below the loop.

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++)
  printf("i = %d", i);
if (i == 10) // NOT VALID IN GCC SINCE 'i' SHOULDN'T EXIST HERE!!!
  printf("the loop executed 10 times\n");

MSVC 7.0 and 7.1 are more ANSI compliant than MSVC 6.0

botman

sfx1999 18-05-2004 18:56

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;
 for (i=0;i<10;i++)
 {
        printf("Lalalalalalala");
 }


Pierre-Marie Baty 18-05-2004 19:12

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 ;)

stefanhendriks 18-05-2004 21:54

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

Rick 18-05-2004 23:39

Re: which one is "standard"...
 
I think its changed later because in the c++ book off Stroustrup it will even say its valid...

sfx1999 19-05-2004 00:30

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.