int main () and void main ()

This is a discussion on int main () and void main () within the C++ Programming forums, part of the General Programming Boards category; Technically, the standards do not prevent conforming implementations (whether freestanding or hosted) from supporting "void main()", or any other entry ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Technically, the standards do not prevent conforming implementations (whether freestanding or hosted) from supporting "void main()", or any other entry point into a program, as long as it supports "int main()".
    The C++ standard requires that the global main() function have a return type of int on hosted environments, though of course int itself is implementation defined. I suppose it is true that it does not explicitly forbid implementations from providing void main() as a non-standard extension, but as you point out, code that uses such an extension would be non-compliant with the standard.

    So, while a compliant implementation is allowed to support void main(), any code which employs that feature is non-compliant with the standards.
    However, if that (now anonymous) author's interpretation of the C standard is correct, then C code that uses void main() complies with the letter of the C standard, though it is not guaranteed by the standard to be portable.
    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

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by laserlight
    However, if that (now anonymous) author's interpretation of the C standard is correct, then C code that uses void main() complies with the letter of the C standard, though it is not guaranteed by the standard to be portable.
    The offending section from the C99 standard is;
    5.1.2.2.1 Program startup
    1 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;9) or in some other implementation-defined manner.
    [/code]
    where the 9) on the last line is a reference to footnote 9, which reads
    9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
    I suppose your anonymous author is saying that, if the code examples and footnote reference are removed the sentence becomes "It shall be defined with a return type of int and with no parameters 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) or equivalent; or in some other implementation-defined manner."

    If so, I disagree with your anonymous author's interpretation. A semi-colon is normally indicative of a continuation in formal reports (and an accepted standard is categorised as a formal report). For example, it will be used with a bulleted list to indicate that one item is related to another. Which means, in this sentence, that the semi-colon might as well not be there.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by grumpy
    The offending section from the C99 standard is;

    where the 9) on the last line is a reference to footnote 9, which reads


    I suppose your anonymous author is saying that, if the code examples and footnote reference are removed the sentence becomes "It shall be defined with a return type of int and with no parameters 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) or equivalent; or in some other implementation-defined manner."

    If so, I disagree with your anonymous author's interpretation. A semi-colon is normally indicative of a continuation in formal reports (and an accepted standard is categorised as a formal report). For example, it will be used with a bulleted list to indicate that one item is related to another. Which means, in this sentence, that the semi-colon might as well not be there.
    I'm a little off, per my usual, but I think I once saw it summed up as: footnotes are non-normative, and C90 did not contain the ending verse. Or something to that respect.

    But since C++ is based on C90, to a degree, that is the standard that applies to C++. So removing the C99 portion you may be left with the foundation with does not quite state that.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You're quite correct Dave. I was, however, responding to laserlights comments about an interpretation of the C99 standard. It may be a little OT in a C++ forum, but his comments were interesting to me (note to self: get out more!) and I didn't feel like breaking off and creating a thread in the C forum to pursue it.

    In C89/90 and C++, the comments I made earlier apply. That extra snippet in the C99 standard muddies the water somewhat, but still (IMHO) doesn't make it legitimate for a programmer to use void main(), particularly if portability matters.

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

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 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