.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
which one is "standard"...
Old
  (#1)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default which one is "standard"... - 16-05-2004

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 ?
  
Reply With Quote
Re: which one is "standard"...
Old
  (#2)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: which one is "standard"... - 16-05-2004

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


  
Reply With Quote
Re: which one is "standard"...
Old
  (#3)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: which one is "standard"... - 16-05-2004

s/"...so which one is the 'standard' one ?"/"...so which one is the 'standard' one, MSVC or GNU ?"
  
Reply With Quote
Re: which one is "standard"...
Old
  (#4)
sPlOrYgOn
<-- He did it.
 
sPlOrYgOn's Avatar
 
Status: Offline
Posts: 1,558
Join Date: Jan 2004
Location: Los Angeles, California, USA, North America, Earth, Solar System, Milky Way.
Default Re: which one is "standard"... - 16-05-2004

what do you mean by standard??
as long as MSVC and GCC can interpret it correct it'll work just fine
  
Reply With Quote
Re: which one is "standard"...
Old
  (#5)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: which one is "standard"... - 16-05-2004

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

Last edited by botman; 16-05-2004 at 16:16..
  
Reply With Quote
Re: which one is "standard"...
Old
  (#6)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: which one is "standard"... - 18-05-2004

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");
 }
  
Reply With Quote
Re: which one is "standard"...
Old
  (#7)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: which one is "standard"... - 18-05-2004

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



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: which one is "standard"...
Old
  (#8)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: which one is "standard"... - 18-05-2004

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!


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote
Re: which one is "standard"...
Old
  (#9)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: which one is "standard"... - 19-05-2004

I think its changed later because in the c++ book off Stroustrup it will even say its valid...
  
Reply With Quote
Re: which one is "standard"...
Old
  (#10)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: which one is "standard"... - 19-05-2004

Well, C99 came out in 1999, which I believe is after the release of MSVC++. It is probably newer than your book, too.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com