Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can main() be overloaded??

can main() be overloaded??

Scheduled Pinned Locked Moved C / C++ / MFC
question
29 Posts 10 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DrFrankenstein90

    A main function returning void is invalid in both C and C++, end of story, no matter that you care about what your program returns or not. (In C++, if you don't, just omit the return statement; in C, you can't omit it, just return 0.) A compiler that allows a main returning void breaks portability to other compilers (GCC, Intel). This is clearly stated in ISO's C++ specification (section 3.6.1; also answering the original poster's question):

    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.

    See also: C++ FAQ Lite [29.3] Should I use void main() or int main()? and Stroustrup: Can I write "void main()"?.

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #21

    DrFrankenstein90 wrote:

    ...no matter that you care about what your program returns or not.

    You're right. How silly of me. What could I have possibly been thinking. :rolleyes:

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Man who follows car will be exhausted." - Confucius

    1 Reply Last reply
    0
    • M MarvinMartian

      If you define it as void nothing is returned. If you define it as int and don't "return" or "exit(something)" then it's going to be God knows what and totally meaningless. 8^)

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #22

      False. The standard explicitly says that if you omit the return statement, main must return 0. Older compiler didn't do that, bat that was their fault. void main() is not C++. It is a Ms extension, that have been adopted also by very few other compiler designed for windows environment. whither you define main (int or void) the system cannot know (it is not statically linked to your program) and always expect an int to be returned. if you return void your main function, simply doesn't write any value in the bottom of the stack, that is left as the OS created it (i.e. with garbage inside).

      2 bugs found. > recompile ... 65534 bugs found. :doh:

      M 1 Reply Last reply
      0
      • P Paul Michalik

        No, see 3.6.1.1. and 3.6.1.2 of the c++ language specification.

        E Offline
        E Offline
        Emilio Garavaglia
        wrote on last edited by
        #23

        Irrelevant. The question is another: can the OS invoke something different than main? That does not depend on C++ specs, but on how the linker is instructed. What about WinMain ?!?

        2 bugs found. > recompile ... 65534 bugs found. :doh:

        P 1 Reply Last reply
        0
        • E Emilio Garavaglia

          False. The standard explicitly says that if you omit the return statement, main must return 0. Older compiler didn't do that, bat that was their fault. void main() is not C++. It is a Ms extension, that have been adopted also by very few other compiler designed for windows environment. whither you define main (int or void) the system cannot know (it is not statically linked to your program) and always expect an int to be returned. if you return void your main function, simply doesn't write any value in the bottom of the stack, that is left as the OS created it (i.e. with garbage inside).

          2 bugs found. > recompile ... 65534 bugs found. :doh:

          M Offline
          M Offline
          MarvinMartian
          wrote on last edited by
          #24

          Well I didn't go into as detailed a explanation but the bottom line is the same... garbage for a return value.

          A 1 Reply Last reply
          0
          • E Emilio Garavaglia

            Irrelevant. The question is another: can the OS invoke something different than main? That does not depend on C++ specs, but on how the linker is instructed. What about WinMain ?!?

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            P Offline
            P Offline
            Paul Michalik
            wrote on last edited by
            #25

            No. The question was: "Can main() be overloaded?". The answer is no. However, the standard does not enforce the existence of "main" - it leaves it up to the implementation how the entry point of a program is defined. But if the entry point is main, "it shall not be overloaded"...

            E 1 Reply Last reply
            0
            • T tsafdrabytrals

              not sure about overloading, but it can be called just like a regular function. 12 Days o Christmas

              P Offline
              P Offline
              Paul Michalik
              wrote on last edited by
              #26

              ...not in a "well formed c++ program" according to c++ standard.

              1 Reply Last reply
              0
              • P Paul Michalik

                No. The question was: "Can main() be overloaded?". The answer is no. However, the standard does not enforce the existence of "main" - it leaves it up to the implementation how the entry point of a program is defined. But if the entry point is main, "it shall not be overloaded"...

                E Offline
                E Offline
                Emilio Garavaglia
                wrote on last edited by
                #27

                Please read all the post: just four line up yours, the OP clarified we where all misleading his originally malposed question!

                2 bugs found. > recompile ... 65534 bugs found. :doh:

                1 Reply Last reply
                0
                • M MarvinMartian

                  Well I didn't go into as detailed a explanation but the bottom line is the same... garbage for a return value.

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #28

                  Sounds like you're misunderstanding Emilio. In case you missed the earlier posts... main() is special. - you can't call it - it always returns an integer - if there isn't an explicit return statement the compiler inserts an effective "return 0;" on that control path So the return value is never "garbage" it's always well defined - either by the programmer or the standard.

                  M 1 Reply Last reply
                  0
                  • A Aescleal

                    Sounds like you're misunderstanding Emilio. In case you missed the earlier posts... main() is special. - you can't call it - it always returns an integer - if there isn't an explicit return statement the compiler inserts an effective "return 0;" on that control path So the return value is never "garbage" it's always well defined - either by the programmer or the standard.

                    M Offline
                    M Offline
                    MarvinMartian
                    wrote on last edited by
                    #29

                    I'm not misunderstanding anything. Of course it returns an int it is simply garbage therefore useless.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups