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; Whoa, you are totally right O_o I did not know that. Also, if you have more than 256 exits, there ...

  1. #16
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,283
    Whoa, you are totally right O_o

    I did not know that.

    Also, if you have more than 256 exits, there is something horribly, horribly wrong with your code or you need to spread it out to multiple executables.

  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    These void main topics became boring. What about a script which would filter out all topics containing void main anywhere in the text...
    Elkvis and Sebastiani like this.
    I never put signature, but I decided to make an exception.

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    1,340
    Quote Originally Posted by jimblumberg View Post
    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).
    MS-DOS is the same, except the limitation was due to the fact program exit code was handled by using "terminate with return code", INT 21H with AH = 4CH and AL = to exit code, with only an 8 bit register (AL) to return the exit code. I don't recall CP/M having exit codes, programs terminated by jumping to zero.
    Sebastiani likes this.

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Quote Originally Posted by kmdv View Post
    What about a script which would filter out all topics containing void main anywhere in the text...
    That would put an unfair limitation on legitimate uses of "void main()" (e.g. code for many embedded devices). Granted, a lot of these devices are moving towards a declaration of "int main()", but I still see no reason to blanket-ban a potential code construct simply because it's not right to use most of the time (as opposed to all of the time).

  5. #20
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by Matticus View Post
    That would put an unfair limitation on legitimate uses of "void main()" (e.g. code for many embedded devices). Granted, a lot of these devices are moving towards a declaration of "int main()", but I still see no reason to blanket-ban a potential code construct simply because it's not right to use most of the time (as opposed to all of the time).
    Technically, most embedded software is not written in C, but in a language that looks and behaves exactly like C. Embedded software relies on "undefined" behavior all the time. This is the boundary between the abstract and the concrete.

    According to the standard, doing something undefined could cause literally anything to happen. On an actual and specific system, what will happen can often be predicted and relied on (and in fact you must, if you ever hope to get anything accomplished).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    You speak true, and I took that into consideration before posting. However, I was not commenting on whether embedded device compilers are or are not strictly compliant to the standard. I was speaking to the comment (facetious or otherwise) that particular flavors of C should be outright banned on the basis that those flavors share a common idiom with an oft-used mistake in standard C coding.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,270
    In the case of embedded software written in C, I believe that if the compiler documents it as such, then void as the return type of the main function conforms to the C standard since that is specified to be entirely implementation defined. In C++, I believe it is an all or nothing proposition: the compiler either provides for a startup function with a different name, or conforms to the requirements for the global main function as if it were not a freestanding environment, otherwise the behaviour is undefined with respect to the standard.
    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

  8. #23
    Registered User
    Join Date
    Oct 2013
    Posts
    12
    Simply because it's the standard.

Page 2 of 2 FirstFirst 12
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