Hi, a Lecturer told a friend of mine that using int main() was bad practice and that void main() is the correct way for C++. Then told him to learn "ghost" or something, anyone shed any light on this? hehe
Hi, a Lecturer told a friend of mine that using int main() was bad practice and that void main() is the correct way for C++. Then told him to learn "ghost" or something, anyone shed any light on this? hehe
Scorpion-ice
Imperium et Respectus
Your friend may have misheard this lecturer...if not then I believe the man is pretty misguided
But dont take my word for it.....ask Bjarne Stroustrup. I would rather his opinion on what's proper C++ over any lecturer
This is the first time I find someone saying that void main() is better than int main(),
what I know is that the main() function should return a value and that's according to ISO stansards for C++
I use both:
That way my program is always correct.Code:std::pair<int,void> main() { ... }
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Now that was funny.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
My college lecturer also told us that void main() was better, something about the fact that most of the time you don't want something returned, so why force it. It wasn't until I came to this site that I found out he was misguided!
hmmm, i dont believe this lecturer is fit to teach! the operating systems expets error codes to be returned (specifically 'real' operating systems like unix) it is good practice and assists portability to use int main (int argc, char * argv[])
Monday - what a way to spend a seventh of your life
Its not a point of forcing anything...the runtime will return a value regardless so the idea of void being "better" is a bit narrow minded and rather sloppy.....Originally posted by PJYelton
My college lecturer also told us that void main() was better, something about the fact that most of the time you don't want something returned, so why force it. It wasn't until I came to this site that I found out he was misguided!
Jesus....will this subject ever end? I mean I accept its my fault for replying to this....but why are there people out there that still make an issue of this......common sense says return an int, most decent compilers freak if you do otherwise, and the standards for C and C++ explicitly say return an int...but still there are people who prefer void?? Maybe I'm being cranky...Maybe I nead a break....
I would say, there will always be people who like to do things the wrong way, and all we can do is make sure that anyone who believes them is told the truth.. don't bother trying to convince those who don't want to listen though there's no point.
James G. Flewelling
Rgistered Linux User #327359
Athabasca University Student (BSc. CIS)
http://catb.org/~esr/faqs/smart-questions.html
http://catb.org/jargon/
http://www.ebb.org/ungeek
---GEEK CODE---
Version: 3.12
GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
----/GEEK CODE----
upd: 2005-02-11
I'm just waiting for someone credible to post anything detailing why 'int main()' is incorrect.
Ain't gonna happen.
Take that break anyway, Fordy!
P.S. You do sound a little cranky, btw.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
From C Runtime as per VC++
Code:mainret = main(__argc, __argv, _environ);//return is assigned... //..... exit(mainret);//...and used in call to exit()
As per CodeWarrior (sorry couldnt find runtime code so ASM)
As per Mingw (again...I cant get the damn debugger to work, so I used Ollydbg and had to track the ASM)Code:call main (0x401000)//return is stored in EAX register pop ecx//not relevant to this...just restoring stack pop ecx//not relevant to this...just restoring stack push eax//push return of main on stack for function call call exit (0x4022da)//call exit with main return as argument
3 different compilers.......3 times, the return of main (or at least what main should be the return) is passed back to the system.......so if you simply MUST pass back a return.....its common sense to have your main function returning whats expected of it....Code:CALL Test.00401210 //This is the main func MOV EBX,EAX//save return in eax ADD ESP,20//Not relevant CALL <JMP.&msvcrt._cexit>//Not relevant ADD ESP,-0C//Not relevant PUSH EBX//push main return on stack CALL <JMP.KERNEL32.ExitProcess>//Call the API-ExitProcess with return of main
If you dont agree......well......ugh....fair enough......go against common sense and the standards...see if I care!!!!
Well, okay, darn it! I'm now convinced.
Take a break. Go on holiday. Do anything other than working on this topic!
(Good Lord, you're starting to make me edgy, and I'm too old to be "edgy". It messes with my karma...whatever that is. )
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
I think Prelude wrote somewhere that main will always return an int, so you don't have to explicitly return 0, but you do need to declare int main() (with or w/o the argc/argv arguments)
Truth is a malleable commodity - Dick Cheney
May have been the only time that I remember Prelude having to retract on a post.
She did qualify that C++ returns '0' by default, unlike C, which is where her thinking was at that time.
(I've lost track of how many times I've had to correct her posts. *cough* )
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
In c++ that's correct but bad practice since it won't
work with older compilers.
The only thing I can think of that makes sense is hosted environment. The c standard defines what a freestandingThen told him to learn "ghost" or something, anyone shed any light on this? hehe
environment and hosted environment are. In a freestanding
environment startup is implementation defined. From what
the standard says I think it's plausable that if you are programming on a freestanding environment int main() maybe invalid but void main() might be correct. You can get
a draft at http://www.lysator.liu.se/c/