what's the difference between int main() and void main()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorkz
    Banned
    New Member
    • Aug 2007
    • 14

    what's the difference between int main() and void main()?

    hlo, i've got a question , some people here gived me advice to use int main(),actually , i dont know what int main() is,but in my manual that i read ,the writer of the book says that i've got to use void main().
    dont know what the problem is , help me!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dorkz
    hlo, i've got a question , some people here gived me advice to use int main(),actually , i dont know what int main() is,but in my manual that i read ,the writer of the book says that i've got to use void main().
    dont know what the problem is , help me!
    The difference is simple: 'void main()' is just plain wrong. Per the Standard the
    return type of main should be int; the int value is used to supply the exit value
    of the process that was started by invoking that same 'int main()' function.

    It was Microsoft that started this 'void main()' nonsense because the return
    values of their processes are very often ignored (for reasons that are beyond me),
    but keep in mind: int main good, void main bad; and spread the word.

    kind regards,

    Jos

    Comment

    • goelvivek
      New Member
      • Dec 2006
      • 26

      #3
      if you assign void main then it means that main function will not return any thing and you did not have to specify of return in main
      if you assign int main then it means that main function will return a int value and you should specify a return statement in the main function otherwise you will get a warning by the compiler

      and other thing is that some 32 bit compiler doesn't support void main and you have to specify int main in them

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by goelvivek
        and other thing is that some 32 bit compiler doesn't support void main and you have to specify int main in them
        Those are called 'compliant' compilers, i.e. they comply with the Standard. Never
        trust a C compiler that accepts 'void main()' without even a warning diagnostic.

        kind regards,

        Jos

        Comment

        • dorkz
          Banned
          New Member
          • Aug 2007
          • 14

          #5
          AH, you mean that void dont exist , that i must use int ?? and that int main() the head function is?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dorkz
            AH, you mean that void dont exist , that i must use int ?? and that int main() the head function is?
            Yep, void main() is just wrong. Here is what the C99 Standard has to say about it:

            Originally posted by C
            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:

            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):

            int main(int argc, char *argv[]) { /* ... */ }

            or equivalent;8) or in some other implementation-defined
            manner.

            [#2] If they are declared, the parameters to the main
            function shall obey the following constraints:

            -- The value of argc shall be nonnegative.

            -- argv[argc] shall be a null pointer.

            -- If the value of argc is greater than zero, the array
            members argv[0] through argv[argc-1] inclusive shall
            contain pointers to strings, which are given
            implementation-defined values by the host environment
            prior to program startup. The intent is to supply to

            _______________ _____

            8) 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.
            All the 'shall clauses' are considered mandatory.

            kind regards,

            Jos

            Comment

            Working...