Thread: void main()

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    void main()

    consider this code:

    Code:
    #include<iostream.h>
    
    using namspace std;
    
    void main()
    {
    cout << "Look at me I am a pig!" << endl;
    return;
    }
    anyway, my mate has a very old borland compiler I think its 2.2 or somthing, and he compiles that and it works fine.

    but i was told if you voided main, the compiler would have a fit at you, and tell you that main must return an int.

    It will not work on my compiler or others, but why was it accepted on that compiler?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Because that compiler isn't fully compliant with the standard.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Just because the compiler doesn't complain, doesn't mean the code is correct.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you answered your own question, because its old

  5. #5
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    yup, void main() is a defunct usage.

    silly really, unless the app crashes, main only closes clean, requiring it to return a clean exit signal is a waste.

    having the option to return or not would be better. but the cpu cycles to make sure everything shut down to be able to return a clean shutdown code is only really usefull during development and testing. once you are in production that's just wasting cpu cycles, as it should be closing clean if you coded and tested correctly.

    just my 2 bits.

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    and its a good point but no matter how well its written someone
    somewhere will crash it for whatever reason.

  7. #7
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    yup.

    which is why the return was made a requirement.

    as rich cook put it:
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this is what my compiler says about it:
    jshao@MCP ~/Programming/C++ $ g++ test.cpp -Wall -W -ansi -o test.exe
    test.cpp:4: error: `main' must return `int'
    test.cpp:4: error: return type for `main' changed to `int'
    and yes, those are errors, not warnings--compilation isn't completed.

    and just for reference, the code:
    Code:
    #include<iostream>
    
    void main()
    {
            std::cout<<"hello world"<<std::endl;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM