Computer HardwareXbox GamesGameCubePlaystation 2PSOnePC/Windows GamesGameboy AdvanceDreamcastNintendo 64Gameboy ColorNintendo DSSony PSPXbox 360Nintendo Wii GamesPS3 Games

Neoseeker Forums » Programming and Design » General Programming » Different between “void main()” and “int main()” (C++)

REPLY TO THIS THREAD   START NEW THREAD
| Sharemore
Options: Print   subscribe   remove   PM this thread to a friendNeoPM  
subscribe to thread Topic: Different between “void main()” and “int main()” (C++)
tom111
(insert witty comment)
Hooked on Neo



tom111's profiletom111's neohome
total posts: 4147
since: Apr 2004
May 23, 09 at 11:55am
Different between “void main()” and “int main()” (C++)

Hey, this may sound like a dumb question, but when using C++ I use both of them for the same thing and get the same result, what is the difference and what are they used for (“void” and “int” bit before the block of code). Here is an example of what I mean

#include <iostream>
int main() {
int iA[2];
iA[0] = 1;
iA[1] = 2;
std::cout << iA[0] << iA[1] << std::endl;
}


#include <iostream>
void main() {
int iA[2];
iA[0] = 1;
iA[1] = 2;
std::cout << iA[0] << iA[1] << std::endl;
}

Also I know there is a C++ thread, however it seems dead and I have a much better chance at getting a reply to this.
Thanks in advance.


-------------------
BSc Computer Science student @ Staffordshire University
quote   quick quote   edit   quick edit   del  searchposts in thread  report
overKill
Resident Neo

overKill's profileoverKill's neohomeNeoPM overKilloverKill's gallery (42 images)
total posts: 3189
since: Dec 2008
May 23, 09 at 9:05pm
re: Different between “void main()” and “int main()” (C++)

    People ask me this a lot, actually.

    main() is a special function that returns to the OS a value signifying if it completed successfully or not (0 being success, any other being typically an error). The OS usually doesn't act on that, but other programs waiting for yours might. The C and C++ standards have specified that main() should return an int-compatible type (which void is not).

quote   quick quote   edit   quick edit   del  searchposts in thread  report
LinkMaster03
NeoXtreme



LinkMaster03's profileLinkMaster03's neohomeNeoPM LinkMaster03
total posts: 8680
since: Jul 2003
May 24, 09 at 5:12pm
re: Different between “void main()” and “int main()” (C++)

int main() returns an integer value, void main() returns no value.



-------------------
"Give a man a link, and he'll have information for a day, teach him to Google and he'll never bug you again."

The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
quote   quick quote   edit   quick edit   del  searchposts in thread  report
Siphon
true seeker

Siphon's profileSiphon's neohomeNeoPM Siphon
since: Feb 2009
May 24, 09 at 7:01pm
re: Different between “void main()” and “int main()” (C++)

quote LinkMaster03
int main() returns an integer value, void main() returns no value.


Yeah, and just use void main() unless you are going to use the return value for something (you won't yet).
quote   quick quote   edit   quick edit   del  searchposts in thread  report
overKill
Resident Neo

overKill's profileoverKill's neohomeNeoPM overKilloverKill's gallery (42 images)
total posts: 3189
since: Dec 2008
May 25, 09 at 12:42am
re: Different between “void main()” and “int main()” (C++)

quote Siphon
quote LinkMaster03
int main() returns an integer value, void main() returns no value.


Yeah, and just use void main() unless you are going to use the return value for something (you won't yet).
    Did you NOT read what I said?
quote   quick quote   edit   quick edit   del  searchposts in thread  report
LinkMaster03
NeoXtreme



LinkMaster03's profileLinkMaster03's neohomeNeoPM LinkMaster03
total posts: 8680
since: Jul 2003
May 25, 09 at 12:54am
re: Different between “void main()” and “int main()” (C++)

Lol, Siphon.

Just have main() return 0, as overKill said.


-------------------
"Give a man a link, and he'll have information for a day, teach him to Google and he'll never bug you again."

The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
quote   quick quote   edit   quick edit   del  searchposts in thread  report
Siphon
true seeker

Siphon's profileSiphon's neohomeNeoPM Siphon
since: Feb 2009
May 25, 09 at 2:04am
re: Different between “void main()” and “int main()” (C++)

Meh, unless your IDE uses it, you don't need it.
quote   quick quote   edit   quick edit   del  searchposts in thread  report
Liam
Relentless



Liam's profileLiam's neohomeNeoPM Liam
total posts: 9144
since: Dec 2001
May 25, 09 at 12:05pm
re: Different between “void main()” and “int main()” (C++)

It is generally good practice to use int main() and return 0.

quote   quick quote   edit   quick edit   del  searchposts in thread  report
overKill
Resident Neo

overKill's profileoverKill's neohomeNeoPM overKilloverKill's gallery (42 images)
total posts: 3189
since: Dec 2008
May 25, 09 at 6:33pm
re: Different between “void main()” and “int main()” (C++)

quote Liam
It is generally good practice to use int main() and return 0.
    What he said sums what I said up, in simple terms.

    But before you read what he said, read what I said so you'll know WHY to use int main() and return 0. The Operating System is expecting a return value, so you have to use int main() and return 0.
quote   quick quote   edit   quick edit   del  searchposts in thread  report
Bigfellahull
newseeker



Bigfellahull's profileEmail BigfellahullNeoPM Bigfellahull
total posts: 16
since: Jul 2009
Jul 14, 09 at 8:33pm
re: Different between “void main()” and “int main()” (C++)

It is such a bad idea to use void main(). In fact - it could potentially be the mistake you make which will give you (or others) the biggest headache.

Not returning a value could cause stack corruption in the programs exit sequence, causing a... you guessed it... crash.

It's also possible that some random value is passed back to the OS. So for example, in a working environment, if a tester was investigating a crash on your code, a main function that returns void, won't be able to guarantee that a non zero return value indicates failure. This can be a major headache inducing scenario.

So, please, please, please stick to standards. They are there for a reason.



-------------------
quote   quick quote   edit   quick edit   del  searchposts in thread  report
[All dates in (PT) time]Threads List   « Next Newest   Next Oldest »
REPLY TO THIS THREAD   START NEW THREAD


search:
search posts by username:
Neoseeker Forums » Programming and Design » General Programming » Different between “void main()” and “int main()” (C++)



Jump to another forum:

Powered by neoforums v0.9.8b (equilibrium)
Copyright Neo Era Media, Inc. 1999-2009

neoseeker forum community
Neoseeker.com   |   Forum Rules   |   Forum FAQ   |   Neoseeker Terms of Use   |   Supermods On Duty [ server id: nova ··· elapsed: 0.1392629147]
Affiliated sites:   GameGrep - Football Manager Wiki - Halo Wiki - MGS Wiki - GTA Wiki - Smackdown Wiki - Zelda Wiki - PS2seeker - Xbox seeker - DEVPEN - GFXcess