Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is it possible to have two main functions in a c program?

Reply
Thread Tools

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

 
 
Walter Roberson
Guest
Posts: n/a
 
      04-03-2008
In article <ft39tj$b0o$(E-Mail Removed)>, jacob navia <(E-Mail Removed)> wrote:
>Walter Roberson wrote:
>> In article <ft36q8$v8k$(E-Mail Removed)>, jacob navia <(E-Mail Removed)> 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(1;

main

> %(2);

"This is main 18"

20
> genmain(17);genmain(1;

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(1; %%(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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-03-2008
jacob navia wrote:
> Walter Roberson wrote:
>> In article <ft36q8$v8k$(E-Mail Removed)>, jacob navia <(E-Mail Removed)>
>> 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.
 
Reply With Quote
 
 
 
 
Dann Corbit
Guest
Posts: n/a
 
      04-03-2008
"jacob navia" <(E-Mail Removed)> wrote in message
news:ft39tj$b0o$(E-Mail Removed)...
> Walter Roberson wrote:
>> In article <ft36q8$v8k$(E-Mail Removed)>, jacob navia <(E-Mail Removed)>
>> 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

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-03-2008
In article <(E-Mail Removed)>,
Robbie Hatley <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-03-2008
mike-yue <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      04-03-2008
On Thu, 03 Apr 2008 20:23:08 +0000, Walter Roberson wrote:
> In article <(E-Mail Removed)>, Robbie
> Hatley <(E-Mail Removed)> 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.
 
Reply With Quote
 
bc1891@googlemail.com
Guest
Posts: n/a
 
      04-03-2008
On Apr 3, 7:11*pm, mike-yue <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
mike-yue
Guest
Posts: n/a
 
      04-03-2008
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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      04-03-2008
In article <26bc7$47f542f5$541dfcd3$(E-Mail Removed)1.nb.ho me.nl>,
Harald van Dijk <(E-Mail Removed)> 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
 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      04-03-2008
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 wre 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$(E-Mail Removed)-online.net>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to have two main methods in a java program ? aks_java Java 6 06-10-2009 03:16 PM
Re: Do i have to have a main function? Keith Halligan C++ 3 05-24-2007 09:49 AM
Do i have to have a main function? PencoOdStip@gmail.com C++ 5 05-24-2007 09:45 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments