Like Tree4Likes

Why int main and not void main?

This is a discussion on Why int main and not void main? within the C++ Programming forums, part of the General Programming Boards category; Looking online, the only answer seems to be "because void main is wrong", with no explanation of why it's wrong. ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    19

    Why int main and not void main?

    Looking online, the only answer seems to be "because void main is wrong", with no explanation of why it's wrong. Does something about a program not work, or function differently if void main is used?

    The only reason I, at this moment, can think of to use int rather than void is because some compilers might not accept void main, but if yours does, then it's really a !issue.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    It's wrong because the C standard says it's wrong. There is absolutely no reason to break portability for an issue like this IMO.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    19
    Quote Originally Posted by SirPrattlepod View Post
    It's wrong because the C standard says it's wrong. There is absolutely no reason to break portability for an issue like this IMO.
    Not conforming to a standard wouldn't make it wrong, just not-conforming. If it caused some erroneous functioning, I think that would make it wrong.


    >> FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    Thanks.


    I also find the 3 responses starting at #6, here, to be pretty good justification for choice of practice:
    Int Main(void) Vs Void Main - C And C++ | Dream.In.Code

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    One issue is the effect on batch files or scripts. The value returned by main is treated as a program's "exit code". Depending on the operating system, batch files and/or scripts can make use of program exit codes to conditionally change which steps of the batch file or script are used. Typically it's for error handling, but for some combinations of programs and batch files / scripts, the exit codes are used for control. On some operating systems, the batch or script process will be aborted if the exit code is "out of range".

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by Delicieuxz
    Why int main and not void main?
    For the "why", my guess is along the same lines as rcgldr's explanation.

    Quote Originally Posted by Delicieuxz
    Not conforming to a standard wouldn't make it wrong, just not-conforming. If it caused some erroneous functioning, I think that would make it wrong.
    Technically, not conforming in this case results in undefined behaviour. One characteristic of undefined behaviour is that it may appear to work, but it might not after compiling again with say, some change in compile options, or when the compiler is upgraded, etc. Hence, it is wrong.

    However, if a compiler documents the behaviour, then it working would be guaranteed, so long as you recognise that you are coding to a particular implementation. In this case, there is no reason to code to a particular implementation. Where it might make sense for you to code to a particular implementation is say, when an implementation provides for a main function with more arguments, e.g., a third argument for environment variables.
    Ducky likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    I thought even void functions technically had a return status.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Quote Originally Posted by MutantJohn View Post
    I thought even void functions technically had a return status.
    A function that is void returns normally, but what you can't depend on is the caller's stack having room for a return value (because the function returns to the caller), or there being a return value that makes sense on the stack. All sorts of weirdness can happen if you intended for there to be a return value and you use a function with a void return type that way.

    And a stack frame is not necessarily implemented in such a way that it depends on secret values ever being used for void return types.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    Quote Originally Posted by MutantJohn View Post
    I thought even void functions technically had a return status.
    In many cases, such as Intel X86, ARM, 68000 series, ... the return value is returned via a register. In the case of a void function, the return value could be whatever was left over from previous operation. The behavior after that depends on what the operating system, and or any currently running batch file or script does with program exit codes.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Quote Originally Posted by Delicieuxz View Post
    Not conforming to a standard wouldn't make it wrong, just not-conforming.
    Only if you're in a situation where conformance against that standard is not required.

    The purpose of a standard is to be a yardstick against which correctness is assessed. If conformance is an essential or contracted requirement, then non-conformance is a breach of requirement. That is usually considered to be wrong in various ways: an essential requirement not met, a breach of contract, ethically, etc etc.

    In a C forum, it is reasonable to expect that conformance with a C standard would be an expectation, and that non-conformance would be considered wrong.

    Quote Originally Posted by Delicieuxz View Post
    If it caused some erroneous functioning, I think that would make it wrong.
    void main() technically invokes undefined behaviour according to the standard. Anything is allowed to happen, including erroneous behaviour.

    Code using void main() will typically result in some form of erroneous behaviour if compiled with a compiler that does not support void main(). The three types of real-world consequences include a compilation error (compiler reports an error), a linker error (the executable cannot be built), or a runtime error (the program can be built, but does not run as intended). All of those are reasonably considered erroneous behaviour.


    The reason the standard specifies int main() is to allow the possibility of returning data to the environment in which the program is run (e.g. a shell script that invokes the program). void main() would prevent returning any information to the environment. int main() allows a value to be returned, but neither forces the program to return a value nor forces the environment to interpret that value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    the relevant section of the standard is 3.6.1.2:

    Quote Originally Posted by C++11 Standard
    3.6.1.2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall
    have a return type of type int
    , but otherwise its type is implementation-defined. All implementations shall
    allow both of the following definitions of main:
    Code:
    int main() { /* ... */ }
    and
    Code:
    int main(int argc, char* argv[]) { /* ... */ }
    Why?

    Because the standard says so.
    Code:
    namespace life
    {
        const bool change = true;
    }

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    I thought it was actually so you could run a program and go echo $? and have the output be useful. C was first developed on Unix/Linux, right?

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by MutantJohn View Post
    C was first developed on Unix/Linux, right?
    more likely C was first developed to (re) write Unix

    C (programming language) - Wikipedia, the free encyclopedia
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    I see. Then yes, it would be useful to have a program return a number because it could easily be interpreted by the OS. It'd be C talking to C, if Unix was written in it. You could do a lot with that and it'd give the user a lot of power to run a program, ensure it ran correctly and if it didn't, what did it return as that could indicate failures across the map. If it didn't, you could pipe that into different programs,etc.

    Returning void would remove a lot of that power and it'd limit a lot of things.

    Also, go Unix!!!! Unix best OS 2013!

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    But remember the only things that you are guaranteed to be able to return from main() are 0, EXIT_SUCCESS, EXIT_FAILURE, anything else is implementation defined. For example on Linux the only values you can return from main() are values that will fit in an unsigned char value (0 to 255).

    Jim

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

Similar Threads

  1. Bit confused about void main() vs int main()
    By pyroknife in forum C Programming
    Replies: 9
    Last Post: 01-08-2013, 02:53 AM
  2. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  3. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 07:34 PM
  4. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM

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