differences between int main and void

This is a discussion on differences between int main and void within the C Programming forums, part of the General Programming Boards category; I dont know what´s the problem, but most people in here dont use void main, instead, they use int main, ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Post differences between int main and void

    I dont know what´s the problem, but most people in here dont use void main, instead, they use int main, and actually thy creata a function main that will work with a void main, because main returns nothing, i would like to ask what is the problem they find with void main, and to give good reasons for using int instead of void main.

    /*If you have no good reason and are just going to say stupid things like: "void main sucks because i want to" please dont make me waste my time reading that, ando dont waste yours writing it*/

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You are ill informed - main does return a value. Do a search on google for comp.lang.c regarding this issue, and also look at the faq entry on this board.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    hi louis,

    The C++ standard requires that main() be defined with a return type of int. "void main()" is
    not, and never has been, legal C++. No matter what some compilers chose to promote.
    Why do people promote it?
    1)Because they don't know.
    2)Because a major company used void main() throught their whole documentation
    3) Because instuctors at juco levels and 4-year colleges think that this major company is god and defines the language

    How can you resolve this?
    For main() there is an exception:
    You don't need to return anything. In this case the compiler has to insert a return 0;
    for you. But beware: main() is the only place where you can do this. In every other function with a return value it is prohibited to not actually return something.
    Other return values are

    return EXIT_SUCCESS;
    return EXIT_FAILURE;

    More detailed and useful information for considering void main() vs. int main() -
    http://www.comeaucomputing.com/techtalk/#voidmain
    http://www.comeaucomputing.com/techtalk/#mainexit
    http://www.comeaucomputing.com/techt...aintermination

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    because main returns nothing
    who told you that?

  5. #5
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    From http://www.faqs.org/faqs/C-faq/faq/

    11.12a: What's the correct declaration of main()?

    A: Either int main(), int main(void), or int main(int argc,
    char *argv[]) (with alternate spellings of argc and *argv[]
    obviously allowed). See also questions 11.12b to 11.15 below.

    References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
    416; CT&P Sec. 3.10 pp. 50-51.

    11.12b: Can I declare main() as void, to shut off these annoying
    "main returns no value" messages?

    A: No. main() must be declared as returning an int, and as
    taking either zero or two arguments, of the appropriate types.
    If you're calling exit() but still getting warnings, you may
    have to insert a redundant return statement (or use some kind
    of "not reached" directive, if available).

    Declaring a function as void does not merely shut off or
    rearrange warnings: it may also result in a different function
    call/return sequence, incompatible with what the caller (in
    main's case, the C run-time startup code) expects.

    (Note that this discussion of main() pertains only to "hosted"
    implementations; none of it applies to "freestanding"
    implementations, which may not even have main(). However,
    freestanding implementations are comparatively rare, and if
    you're using one, you probably know it. If you've never heard
    of the distinction, you're probably using a hosted
    implementation, and the above rules apply.)

    References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
    416; CT&P Sec. 3.10 pp. 50-51.

    11.13: But what about main's third argument, envp?

    A: It's a non-standard (though common) extension. If you really
    need to access the environment in ways beyond what the standard
    getenv() function provides, though, the global variable environ
    is probably a better avenue (though it's equally non-standard).

    References: ISO Sec. G.5.1; H&S Sec. 20.1 pp. 416-7.

    11.14: I believe that declaring void main() can't fail, since I'm
    calling exit() instead of returning, and anyway my operating
    system ignores a program's exit/return status.

    A: It doesn't matter whether main() returns or not, or whether
    anyone looks at the status; the problem is that when main() is
    misdeclared, its caller (the runtime startup code) may not even
    be able to *call* it correctly (due to the potential clash of
    calling conventions; see question 11.12b).

    It has been reported that programs using void main() and
    compiled using BC++ 4.5 can crash. Some compilers (including
    DEC C V4.1 and gcc with certain warnings enabled) will complain
    about void main().

    Your operating system may ignore the exit status, and
    void main() may work for you, but it is not portable and not
    correct.

    11.15: The book I've been using, _C Programing for the Compleat Idiot_,
    always uses void main().

    A: Perhaps its author counts himself among the target audience.
    Many books unaccountably use void main() in examples, and assert
    that it's correct. They're wrong.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    yeah Ive been to school like for a month now and it seems my teacher doesnt want to touch the subject and I wouldnt want to further confuse myself also.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i would like to ask what is the problem they find with void main, and to give good reasons for using int instead of void main.
    The standard language definition explicitly states that void main is wrong. Actually, it says that any return value for main except int in a hosted implementation is undefined, and undefined is a Bad Thing. That's enough for me, but some people are just stubborn.

    >If you have no good reason and are just going to say stupid things like: "void main sucks because i want to"
    There are several very good reasons and proof to go with them, but this topic has been discussed at length many times in the past. Do a board search.

    >it seems my teacher doesnt want to touch the subject
    This is an indication that your teacher lacks fundamental knowledge of the subject and doesn't want to be embarrassed by not knowing something.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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