Is it possible to have two main functions in a c program?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mike-yue

    Is it possible to have two main functions in a c program?

    and, Is it possible to call one main function from another main
    function?

    Thanks guys.
  • jacob navia

    #2
    Re: Is it possible to have two main functions in a c program?

    mike-yue wrote:
    and, Is it possible to call one main function from another main
    function?
    >
    Thanks guys.
    In general you can't have two functions with the same name in C
    (or in any other language for that matter)

    If you think about it

    *HOW* would the program recognize which is which if they have the same name?


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

    Comment

    • mike-yue

      #3
      Re: Is it possible to have two main functions in a c program?

      I remember some guy said that it is possible to do it with macro or
      #define something, but I can't find the page anymore.

      Comment

      • Ben Pfaff

        #4
        Re: Is it possible to have two main functions in a c program?

        jacob navia <jacob@nospam.c omwrites:
        In general you can't have two functions with the same name in C
        (or in any other language for that matter)
        >
        If you think about it
        >
        *HOW* would the program recognize which is which if they have the same name?
        For someone who wants to extend C in the direction of C++, you
        are remarkably ignorant of C++ features.
        --
        "Large amounts of money tend to quench any scruples I might be having."
        -- Stephan Wilms

        Comment

        • jacob navia

          #5
          Re: Is it possible to have two main functions in a c program?

          Ben Pfaff wrote:
          jacob navia <jacob@nospam.c omwrites:
          >
          >In general you can't have two functions with the same name in C
          >(or in any other language for that matter)
          >>
          >If you think about it
          >>
          >*HOW* would the program recognize which is which if they have the same name?
          >
          For someone who wants to extend C in the direction of C++, you
          are remarkably ignorant of C++ features.
          Ahh excuse me. I thought we were discussing C here.



          --
          jacob navia
          jacob at jacob point remcomp point fr
          logiciels/informatique

          Comment

          • Ben Pfaff

            #6
            Re: Is it possible to have two main functions in a c program?

            jacob navia <jacob@nospam.c omwrites:
            Ben Pfaff wrote:
            >jacob navia <jacob@nospam.c omwrites:
            >>
            >>In general you can't have two functions with the same name in C
            >>(or in any other language for that matter)
            >>>
            >>If you think about it
            >>>
            >>*HOW* would the program recognize which is which if they have the same name?
            >>
            >For someone who wants to extend C in the direction of C++, you
            >are remarkably ignorant of C++ features.
            >
            Ahh excuse me. I thought we were discussing C here.
            You brought it up when you said: "or in any other language for
            that matter".
            --
            char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
            ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
            =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
            2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

            Comment

            • Walter Roberson

              #7
              Re: Is it possible to have two main functions in a c program?

              In article <ft36q8$v8k$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
              >mike-yue wrote:
              >and, Is it possible to call one main function from another main
              >function?
              >In general you can't have two functions with the same name in C
              >(or in any other language for that matter)
              There are a number of languages (such as maple) that make it trivial
              to have multiple functions with the same name.

              >If you think about it
              >*HOW* would the program recognize which is which if they have the same name?
              Scope.
              --
              "For men are prone to go it blind along the calf-paths of the
              mind To do what other men have done. They follow in the beaten
              track, and out and in, and forth and back, and still their
              devious course pursue, to keep the path that others do." -- Sam Walter Foss

              Comment

              • jacob navia

                #8
                Re: Is it possible to have two main functions in a c program?

                Walter Roberson wrote:
                In article <ft36q8$v8k$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                >mike-yue wrote:
                >>and, Is it possible to call one main function from another main
                >>function?
                >
                >In general you can't have two functions with the same name in C
                >(or in any other language for that matter)
                >
                There are a number of languages (such as maple) that make it trivial
                to have multiple functions with the same name.
                >
                No, the signature of the functions must be different. THAT can't be
                the case with "main"
                >
                >If you think about it
                >
                >*HOW* would the program recognize which is which if they have the same name?
                >
                Scope.
                In that case yes.


                --
                jacob navia
                jacob at jacob point remcomp point fr
                logiciels/informatique

                Comment

                • Ian Collins

                  #9
                  Re: Is it possible to have two main functions in a c program?

                  jacob navia wrote:
                  Walter Roberson wrote:
                  >In article <ft36q8$v8k$1@a ioe.org>, jacob navia <jacob@nospam.o rg>
                  >wrote:
                  >>mike-yue wrote:
                  >>>and, Is it possible to call one main function from another main
                  >>>function?
                  >>
                  >>In general you can't have two functions with the same name in C
                  >>(or in any other language for that matter)
                  >>
                  >There are a number of languages (such as maple) that make it trivial
                  >to have multiple functions with the same name.
                  >>
                  >
                  No, the signature of the functions must be different. THAT can't be
                  the case with "main"
                  >
                  int main(void)
                  int main(int, char**)

                  --
                  Ian Collins.

                  Comment

                  • Dann Corbit

                    #10
                    Re: Is it possible to have two main functions in a c program?

                    "jacob navia" <jacob@nospam.c omwrote in message
                    news:ft39tj$b0o $1@aioe.org...
                    Walter Roberson wrote:
                    >In article <ft36q8$v8k$1@a ioe.org>, jacob navia <jacob@nospam.o rg>
                    >wrote:
                    >>mike-yue wrote:
                    >>>and, Is it possible to call one main function from another main
                    >>>function?
                    >>
                    >>In general you can't have two functions with the same name in C
                    >>(or in any other language for that matter)
                    >>
                    >There are a number of languages (such as maple) that make it trivial
                    >to have multiple functions with the same name.
                    >>
                    >
                    No, the signature of the functions must be different. THAT can't be
                    the case with "main"
                    int main(void)
                    {return 0;}

                    int main(int argc, char **argv)
                    {return 0;}
                    >>
                    >>If you think about it
                    >>
                    >>*HOW* would the program recognize which is which if they have the same
                    >>name?
                    >>
                    >Scope.
                    >
                    In that case yes.
                    >
                    >
                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatique
                    http://www.cs.virginia.edu/~lcc-win32


                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Robbie Hatley

                      #11
                      Re: Is it possible to have two main functions in a c program?


                      "mike-yue" wrote:
                      Is it possible to call one main function from another main function?
                      No. Not in C. There must be exactly ONE main, with one of these two
                      signatures:

                      int main (void)
                      int main (int, char**)

                      Not in C++ either, unless the other "main" is in a different
                      namespace. You can have ::main and Fred::main, but not
                      ::main(sig1) and ::main(sig2). (Can't overload ::main.)

                      Not in Perl, either, unless the two "main" subs are in different
                      packages.

                      Why would you want to do this? Why not call the other function
                      something different, like "Fred" or "Zamboni" or whatever?

                      Plotting your strategy for the next "obfuscated C" contest?
                      Just curious.

                      --
                      Cheers,
                      Robbie Hatley
                      lonewolf aatt well dott com
                      www dott well dott com slant user slant lonewolf slant


                      Comment

                      • Walter Roberson

                        #12
                        Re: Is it possible to have two main functions in a c program?

                        In article <BK2dnZxMLvtTp2 janZ2dnUVZ_jydn Z2d@giganews.co m>,
                        Robbie Hatley <see.my.signatu re@for.my.email .addresswrote:
                        >"mike-yue" wrote:
                        >Is it possible to call one main function from another main function?
                        >No. Not in C. There must be exactly ONE main, with one of these two
                        >signatures:
                        int main (void)
                        int main (int, char**)
                        Citation?

                        'main' is not in the C89 list of reserved identifiers, and the C89
                        section 2.1.2.2.1 Program Startup has no obvious restriction against
                        there being a different main that does not have external linkage.

                        And in a freestanding environment 2.1.2.1 "the name and type of
                        the function called at program startup are implementation-defined.
                        There are otherwise no reserved external identifiers."
                        --
                        "The art of storytelling is reaching its end because the epic
                        side of truth, wisdom, is dying out." -- Walter Benjamin

                        Comment

                        • Harald van =?UTF-8?b?RMSzaw==?=

                          #13
                          Re: Is it possible to have two main functions in a c program?

                          On Thu, 03 Apr 2008 20:23:08 +0000, Walter Roberson wrote:
                          In article <BK2dnZxMLvtTp2 janZ2dnUVZ_jydn Z2d@giganews.co m>, Robbie
                          Hatley <see.my.signatu re@for.my.email .addresswrote:
                          >>"mike-yue" wrote:
                          >>Is it possible to call one main function from another main function?
                          >
                          >>No. Not in C. There must be exactly ONE main, with one of these two
                          >>signatures:
                          >
                          > int main (void)
                          > int main (int, char**)
                          >
                          Citation?
                          >
                          'main' is not in the C89 list of reserved identifiers, and the C89
                          section 2.1.2.2.1 Program Startup has no obvious restriction against
                          there being a different main that does not have external linkage.
                          Right.

                          #1

                          extern double (*mainptr)(void );

                          int main(void) {
                          return (*mainptr)();
                          }

                          #2

                          static double main(void) {
                          return 0;
                          }

                          double (*mainptr)(void ) = &main;

                          These two source files make up a correct C program, demonstrating one
                          function named main called directly by another function with the same
                          name.

                          I suspect that the OP means something different by "main function", but I
                          do not know what.

                          Comment

                          • mike-yue

                            #14
                            Re: Is it possible to have two main functions in a c program?

                            to Keith Thompson:

                            nothing special.
                            Just an interviewer ask me the question.
                            I answered: I've never tried that before. if there are two main
                            functions, the compiler will report errors.
                            After the event, I was wondering maybe there are some other opinions.
                            So I came here, and got so many wonderful answers.
                            I am really happy that this group has all you knowledgable guys here.

                            Comment

                            • Richard Tobin

                              #15
                              Re: Is it possible to have two main functions in a c program?

                              In article <26bc7$47f542f5 $541dfcd3$17366 @cache3.tilbu1. nb.home.nl>,
                              Harald van Dijk <truedfx@gmail. comwrote:
                              >I suspect that the OP means something different by "main function", but I
                              >do not know what.
                              It's possible that the question referred to the practice of combining
                              several programs into one, to save space, which is common on very
                              small systems (and system recovery disks). This works by renaming
                              their main()s and adding a new main() that selects between them based
                              on argv[0].

                              -- Richard
                              --
                              :wq

                              Comment

                              Working...