Void Main?

This is a discussion on Void Main? within the C Programming forums, part of the General Programming Boards category; I read over a dozen times on this site that initialising main() as void is a sin. However our teacher ...

  1. #1
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132

    Void Main?

    I read over a dozen times on this site that initialising main() as void is a sin.
    However our teacher writes all progs. with main as void.If not,then he doesn't initialise it at all!
    What does happen if in practice main is initialised as void.
    PS-Of course ,I can't try it out myself for about a week or two as I'm yet to get a copy of TurboC.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    To be more specific: if you have a good compiler then you'll get an error when trying to compile. If not, then you could face a much worse possibility. Since the OS is expecting something back, what it gets will be undefined so it could be anything, including instructions on how to take over the world and make us all computer slaves.

    Most of the time nothing bad happens, however it still leaves the possibility of something really bad happening. One person on another thread said their computer got wiped due to using void main.

    Try to find a copy of the ANSI C99 standard and email it, fax it, mail it to the instructor. If that fails, drug them, and have it tattoed on them.

  3. #3
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Computer got wiped? !

    How is that possible? I mean what sort of process would react badly to void main??

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Using most compilers, void main() will not actually cause problems, but there are rare compilers or operating systems that can't handle it.

    But just because it won't kill your system, it's still shouldn't be done. Kinda like using ketchup on a chocolate bar-- it won't kill you, but I also wouldn't want to try it.

    The FAQ on this site has a good writeup about it.
    Last edited by WaltP; 09-10-2003 at 06:33 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Think of it this way. The operating system *expects* main to return an integer. So whatever is laying around in the eax register will be interpreted as such. Now if the OS defines the value of say '-6' as a fatal-error return code, and that value *just happens* to be there - guess what? It might just give you the BSOD - who knows! The point is, you would never prototype a function one way and then define it another way later on in the code. That's just not how it's supposed to be done.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Actually void main() can be interpreted to be legal C, according to the latest standard, because of a semantic error in the standard document.
    The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
    Code:
    int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
    Code:
    int main(int argc, char *argv[]) { /* ... */ }
    or equivalent; or in some other implementation-defined manner."
    For more info, check this page:
    http://homepages.tesco.net/~J.deBoyn...void-main.html

    NB: In any case, this is an error in the standard, you should never, never use void main because of this.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >I read over a dozen times on this site that initialising main() as void is a sin.
    It's okay if your compiler allows it as an alternate declaration or you're on a freestanding implementation. The reason everyone says it's evil is because you get unpredictable behavior if the compiler doesn't allow it, and you can't be sure if it does unless you're familiar with it. Then there are those who just follow the herd and say it's evil but don't bother to learn more on the topic.

    >If not,then he doesn't initialise it at all!
    Under the old C standard (and just about everything is still the old standard because almost nobody uses C99) you can declare main like this:
    Code:
    main ( ) {
      return 0;
    }
    And the compiler will assume you mean it to return int.

    >What does happen if in practice main is initialised as void.
    In practice, probably nothing.

    >I'm yet to get a copy of TurboC.
    Get with the times, TurboC is hopelessly outdated and you can find better compilers online for free.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> or equivalent; or in some other implementation-defined manner

    Hmm, for some reason I keep getting errors with -

    Code:
    template <class T>
    T main(void)
    {
    
    }
    Dumb compiler.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Hmm, for some reason I keep getting errors with
    And that has what to do with C programming?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    >What does happen if in practice main is initialised as void.
    In practice, probably nothing.


    In practice, just about anything may happen. Computer programming is about taking control of the computer and make it do your bidding. Giving up control by being lazy and leaving out things that probably won't go wrong most of the time is going to bring chaos and anarchy to your little realm of control in the worst possible moment ( see Murphy ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >In practice, just about anything may happen.
    In theory, just about anything may happen. In practice, 'anything' is typically the correct behavior, or a kludge to save the idiot programmer from him/herself that results in the correct behavior. Assuming 'correct behavior' to the idiot programmer is the same 'correct behavior' to us...you can never tell with idiots.

    >is going to bring chaos and anarchy to your little realm of control in the worst possible moment
    And programming is about excitement. No risk, no excitement, no? I personally love doing this:
    Code:
    void main ( ) {
      printf("Woohoo!");
    }
    And getting a rush of adrenaline when 'find / -exec rm {} \;' isn't called due to undefined behavior.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  12. #12
    Registered User RussMan's Avatar
    Join Date
    Sep 2003
    Posts
    14
    Hmmm all seemed to know it but couldn't get the ball right there. Its as simple as this. (Don't worry about old ansi stanards stick with the current one becuase the OS does). Window expects your program to give it a return value to free its memory space. If you don't return a value windows just simply awesome its still running. Yes it may disappear from the task manager. Yes it appears its not there but here is something it does free the memory space (well most times) so if you have a limited amount of memory you will eventually request memory and your compiler will say "What you expect me to give you any after you just filled me?" So always return a int value in main.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by RussMan
    (Don't worry about old ansi stanards stick with the current one becuase the OS does).
    Bad advice. Always stick with the ANSI standard. That isn't to say don't do anything system-specific, because at times you have to. However, system-specific or not, you still follow the rules set up for you by the standard.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User RussMan's Avatar
    Join Date
    Sep 2003
    Posts
    14
    Alright very good point but when it comes to a simple function standard he should stick to it. If its system specific then sure go ahead but if its not then standards are probably the best thing unless your know your programming language that well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09: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, 01:20 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02: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

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