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

Discussion in 'C Programming' started by mike-yue, Apr 3, 2008.

  1. mike-yue

    mike-yue Guest

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

    Thanks guys.
    mike-yue, Apr 3, 2008
    #1
    1. Advertising

  2. mike-yue

    jacob navia Guest

    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
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Apr 3, 2008
    #2
    1. Advertising

  3. In article <>,
    mike-yue <> wrote:
    >and, Is it possible to call one main function from another main
    >function?


    Hmmm, I think the answer is Yes to both questions. One of the two
    main() would have to have file scope in a seperate unit than the
    standard main() invoked by the operating system. In order for
    one of the main() routines to call the other, it would need to
    somehow get a function pointer that was the other.

    For example:

    File1.c:

    static int main(int argc, char **argv) {
    int (*mainptr)(int, char **) = getmainmain();
    printf("this is the second main\n");
    if (argc > 0)
    (*mainptr)(argc-1,argv);
    }

    int (*)(int, char**) getaltmain(void) {
    return main;
    }


    File2.c:

    int main(int argc, char **argv) }
    int (*mainptr)(int, char **) = getaltmain();
    printf("this is the main main\n");
    if (argc > 0)
    (*mainptr)(argc-1,argv);
    }

    int (*)(int, char**) getmainmain(void) {
    return main;
    }

    --
    "Nothing recedes like success." -- Walter Winchell
    Walter Roberson, Apr 3, 2008
    #3
  4. mike-yue

    mike-yue Guest

    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.
    mike-yue, Apr 3, 2008
    #4
  5. mike-yue

    Ben Pfaff Guest

    jacob navia <> writes:

    > 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
    Ben Pfaff, Apr 3, 2008
    #5
  6. mike-yue

    jacob navia Guest

    Ben Pfaff wrote:
    > jacob navia <> writes:
    >
    >> 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
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Apr 3, 2008
    #6
  7. mike-yue

    Ben Pfaff Guest

    jacob navia <> writes:

    > Ben Pfaff wrote:
    >> jacob navia <> writes:
    >>
    >>> 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);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,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)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Apr 3, 2008
    #7
  8. In article <ft36q8$v8k$>, jacob navia <> 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.


    >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
    Walter Roberson, Apr 3, 2008
    #8
  9. mike-yue

    jacob navia Guest

    Walter Roberson wrote:
    > In article <ft36q8$v8k$>, jacob navia <> 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"

    >
    >> 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
    jacob navia, Apr 3, 2008
    #9
  10. mike-yue <> writes:

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


    It's probably not what you want, but I don't know what your problem is...

    /* file1.c */

    static int main(int argc, char** argv) {
    /* what you want */
    return 0;
    }

    typedef int (*mainFunc)(int argc, char** argv);

    mainFunc get() {
    return main;
    }

    /* file2.c */

    typedef int (*mainFunc)(int argc, char** argv);

    mainFunc get();

    int main(int argc, char** argv) {
    return (*get())(argc, argv);
    }

    Yours,

    --
    Jean-Marc
    Jean-Marc Bourguet, Apr 3, 2008
    #10
  11. In article <ft39tj$b0o$>, jacob navia <> wrote:
    >Walter Roberson wrote:
    >> In article <ft36q8$v8k$>, jacob navia <> 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"


    Counterproof in maple. Note that the signature of each generated main
    is the same:


    > genmain := proc(Y)

    local main;
    main := proc(x) print(cat("This is main ", Y)); x + Y end proc; main
    end proc

    > genmain(17);

    main

    > %(2);

    "This is main 17"

    19

    > genmain(18);

    main

    > %(2);

    "This is main 18"

    20
    > genmain(17);genmain(18);

    main

    main

    > %%(2);%%(2);

    "This is main 17"

    19

    "This is main 18"

    20


    procedures are first-class objects in maple; the 'main' that you
    see returned from each invocation of genmain() is the named procedure.
    We can see from the genmain(17);genmain(18); %%(2);%%(2);
    sequence that multiple procedures with the same name ('main') can
    exist at the same time.
    (Note: in maple, % refers to the last returned result, and %%
    refers to the second-last returned result.)

    I took advantage here of a feature of maple, that each invocation of
    a procedure uses a -different- instance of the named local variables;
    thus even though the two 'main' have the same name, they are distinct
    variables:

    > x - x;

    0

    > gx := proc() local x;x end proc;

    gx := proc() local x; x end proc

    > gx() - gx();

    x - x

    Each gx() returns a distinct variable named 'x', so when the two
    are subtracted in maple, the result remains unevaluated rather than being 0.


    What does this have to do with C? Well when you said "or any other
    language for that matter", I mentioned maple as a counter-example
    and you specifically said "No" to that, so I show here that Yes,
    it really can happen in other languages, and that Yes, in other
    languages the different distinct functions with the same name need
    not have different function signatures.
    --
    "Walter is undoubtedly the country's and club's most popular player."
    -- vitalfootball.co.uk
    Walter Roberson, Apr 3, 2008
    #11
  12. mike-yue

    Ian Collins Guest

    jacob navia wrote:
    > Walter Roberson wrote:
    >> In article <ft36q8$v8k$>, jacob navia <>
    >> 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.
    Ian Collins, Apr 3, 2008
    #12
  13. mike-yue

    Dann Corbit Guest

    "jacob navia" <> wrote in message
    news:ft39tj$b0o$...
    > Walter Roberson wrote:
    >> In article <ft36q8$v8k$>, jacob navia <>
    >> 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
    Dann Corbit, Apr 3, 2008
    #13
  14. In article <>,
    Robbie Hatley <> wrote:

    >"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
    Walter Roberson, Apr 3, 2008
    #14
  15. mike-yue <> writes:
    > and, Is it possible to call one main function from another main
    > function?


    It's best to put the entire question in the body of the article. Some
    newsreaders make it easy to miss the subject header.

    The full question is:

    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?

    Rather than trying to answer your question, I'll ask you another one:

    Why?

    What problem are you trying to solve?

    I suspect that you're trying to solve some deeper problem, and that
    you've assumed that having two main function is the solution, and
    you're asking us how to implement it. Tell us what the problem is,
    and we can probably come up with a cleaner solution. (I have a hunch
    about what it might be, but I'll let you describe it yourself.)

    Either that, or you're just curious about the language rules. There's
    nothing wrong with that, of course, but knowing your goal would help
    us provide meaningful anwsers.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 3, 2008
    #15
  16. On Thu, 03 Apr 2008 20:23:08 +0000, Walter Roberson wrote:
    > In article <>, Robbie
    > Hatley <> wrote:
    >>"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.
    Harald van Dijk, Apr 3, 2008
    #16
  17. mike-yue

    Guest

    On Apr 3, 7:11 pm, mike-yue <> wrote:
    > and, Is it possible to call one main function from another main
    > function?
    >
    > Thanks guys.


    #include <stdio.h>

    int main(void)
    {

    #define main() printf("No, but you can do this, which is equally
    pointless\n")

    main();

    return 0;
    }
    , Apr 3, 2008
    #17
  18. mike-yue

    mike-yue Guest

    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.
    mike-yue, Apr 3, 2008
    #18
  19. In article <26bc7$47f542f5$541dfcd3$1.nb.home.nl>,
    Harald van Dijk <> wrote:

    >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
    Richard Tobin, Apr 3, 2008
    #19
  20. jacob navia schrieb:

    > If you think about it
    >
    > *HOW* would the program recognize which is which if they have the same
    > name?


    Polymorphism!

    Regards,
    Johannes

    --
    "PS: Ein Realname wäre nett. Ich selbst nutze nur keinen, weil mich die
    meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
    Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
    Rosenberg in dse <45608268$0$5719$-online.net>
    Johannes Bauer, Apr 3, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    512
    Andrew Thompson
    Oct 31, 2005
  2. mark4asp
    Replies:
    2
    Views:
    619
    Jasbird
    Nov 11, 2006
  3. Replies:
    5
    Views:
    311
    James Kanze
    May 24, 2007
  4. Keith Halligan

    Re: Do i have to have a main function?

    Keith Halligan, May 23, 2007, in forum: C++
    Replies:
    3
    Views:
    269
    James Kanze
    May 24, 2007
  5. aks_java
    Replies:
    6
    Views:
    2,738
    RedGrittyBrick
    Jun 10, 2009
Loading...

Share This Page