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

M

mike-yue

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

Thanks guys.
 
J

jacob navia

mike-yue said:
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?
 
W

Walter Roberson

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;
}
 
M

mike-yue

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.
 
B

Ben Pfaff

jacob navia said:
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.
 
J

jacob navia

Ben said:
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.
 
W

Walter Roberson

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.
 
J

jacob navia

Walter said:
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"

In that case yes.
 
J

Jean-Marc Bourguet

mike-yue said:
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,
 
W

Walter Roberson

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
"This is main 17"

19
genmain(18);
main
"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:
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.
 
D

Dann Corbit

jacob navia said:
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;}
 
W

Walter Roberson

Robbie Hatley said:
"mike-yue" wrote:
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."
 
K

Keith Thompson

mike-yue said:
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.
 
H

Harald van Dijk

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.
 
B

bc1891

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;
}
 
M

mike-yue

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.
 
R

Richard Tobin

Harald van Dijk said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top