Why does main need to return a value?

This is a discussion on Why does main need to return a value? within the C++ Programming forums, part of the General Programming Boards category; I read the FAQ and it told me that it should always return an int. The thing that I don't ...

  1. #1
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40

    Question Why does main need to return a value?

    I read the FAQ and it told me that it should always return an int. The thing that I don't understand though is why it needs to return anything at all... Can you use something like "!C:\program.exe" to state whether or not it returned TRUE or FALSE? Or is int just the way C++ makes you to do it?

    TIA

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    if you use
    int main()
    you must return a value because the main function states: main returns an int

    if you use
    void main()
    you dont need to return a value

    i think the first one is the standar now.

    i will give you an example how i use the return value of main...

    i have a program that "calls" other c++ programs, to check if that call was succesfull the main functions of each program must return a value, generally 0 if not succesfull and 1 if succesfull.

    hope it helps, and please excuse my poor english

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if you use
    >void main()
    >you dont need to return a value
    But then your code would be wrong and undefined, and that's bad.

    >i think the first one is the standar now.
    main has always returned int, from before day one. The only reason void was accepted on some implementations is because the vendors were braindead or forced into it by stupid customers.

    >The thing that I don't understand though is why it needs to return anything at all...
    First, the operating system might choose to do something different depending on whether the program returns a success or failure code. Second, the calling process might treat different failure codes differently because there are quite a few classes of errors that can occur. Third, you can write a batch file or shell script that takes advantage of the return value of a program that you call (this would be the calling process in that case).

    >Or is int just the way C++ makes you to do it?
    That too. The C++ standard states that main shall return int, and when the standard says "shall", it means you had better do it if you don't like demons flying out your nose. Pre-standard C++ still specified int as the return type of main, but lazy and stupid people thought that void was cooler. As it turns out, void is not cooler. Just in case you were wondering.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,256
    to check if that call was succesfull the main functions of each program must return a value, generally 0 if not succesfull and 1 if succesfull.
    Actually, returning 0 from main() means success.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by jimboob
    I read the FAQ and it told me that it should always return an int. The thing that I don't understand though is why it needs to return anything at all... Can you use something like "!C:\program.exe" to state whether or not it returned TRUE or FALSE? Or is int just the way C++ makes you to do it?

    TIA

    http://www.research.att.com/~bs/bs_faq2.html#void-main

  6. #6
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40
    Cool, thanks everyone.
    I never knew that you could access the return value of main from another program. That could help heaps with the project that i'm currently working on.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    sorry my mistake 0=succesfull
    thanks bithub

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 12:52 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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