College Lecturer

This is a discussion on College Lecturer within the C++ Programming forums, part of the General Programming Boards category; Hi, a Lecturer told a friend of mine that using int main() was bad practice and that void main() is ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    College Lecturer

    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

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    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++

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I use both:
    Code:
     
    std::pair<int,void> main()
    {
      ...
    }
    That way my program is always correct.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708


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

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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!

  7. #7
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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!
    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.....

    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....

  9. #9
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    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

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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)

    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
    As per Mingw (again...I cant get the damn debugger to work, so I used Ollydbg and had to track the ASM)

    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
    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....

    If you dont agree......well......ugh....fair enough......go against common sense and the standards...see if I care!!!!

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

  15. #15
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    In c++ that's correct but bad practice since it won't
    work with older compilers.

    Then told him to learn "ghost" or something, anyone shed any light on this? hehe
    The only thing I can think of that makes sense is hosted environment. The c standard defines what a freestanding
    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/

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I go to a state college and be 20k in debt, or a private college and be 90k in
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-10-2003, 08:22 PM
  2. University versus college, which should I do?
    By Terrance11 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-02-2003, 11:19 PM
  3. Which college should I go to?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-13-2003, 10:06 PM
  4. College or No College :: Future
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 09-25-2002, 03:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21