468,457 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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.
Apr 3 '08 #1
17 4269
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
Apr 3 '08 #2
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.
Apr 3 '08 #3
jacob navia <ja***@nospam.comwrites:
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
Apr 3 '08 #4
Ben Pfaff wrote:
jacob navia <ja***@nospam.comwrites:
>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
Apr 3 '08 #5
jacob navia <ja***@nospam.comwrites:
Ben Pfaff wrote:
>jacob navia <ja***@nospam.comwrites:
>>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,0xa6 7f6aaa,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;}}}
Apr 3 '08 #6
In article <ft**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:
>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
Apr 3 '08 #7
Walter Roberson wrote:
In article <ft**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:
>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
Apr 3 '08 #8
jacob navia wrote:
Walter Roberson wrote:
>In article <ft**********@aioe.org>, jacob navia <ja***@nospam.org>
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.
Apr 3 '08 #9
"jacob navia" <ja***@nospam.comwrote in message
news:ft**********@aioe.org...
Walter Roberson wrote:
>In article <ft**********@aioe.org>, jacob navia <ja***@nospam.org>
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

Apr 3 '08 #10

"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
Apr 3 '08 #11
In article <BK******************************@giganews.com>,
Robbie Hatley <se**************@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
Apr 3 '08 #12
On Thu, 03 Apr 2008 20:23:08 +0000, Walter Roberson wrote:
In article <BK******************************@giganews.com>, Robbie
Hatley <se**************@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.
Apr 3 '08 #13
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.

Apr 3 '08 #14
In article <26***************************@cache3.tilbu1.nb.ho me.nl>,
Harald van Dijk <tr*****@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
Apr 3 '08 #15
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 <45**********************@newsspool3.arcor-online.net>
Apr 3 '08 #16

Walter Roberson wrote:
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?
ISO/IEC 9899:1999 (E) section 5.1.2.2.1 :

5.1.2.2.1 Program startup

1 The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the function
in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

Now, you can try to violate that by having functions called
"main" in two different modules, but your compiler is likely
to say some thing like this:

cwd = C:\RHE\src\test
$gcc module1.c module2.c -o C:\bin-test\multmain.exe
module1.c:9: error: conflicting types for 'main'
module1.c:6: error: previous declaration of 'main' was here
module1.c:10: error: too many arguments to function 'main'
module2.c: In function 'main':
module2.c:6: warning: return type of 'main' is not 'int'
cwd = C:\RHE\src\test
$

Yes, maybe you can hide one "main" function by making it "static"
and calling it using pointers. That's what I'd call a "loophole",
however. It thwarts the intentions of the framers of the standard,
and is bad programming. It's like getting around "thou shalt not
commit murder" by working to reduce a person's morale until the
person wants to commit suicide, then handing him some cyanide.

So let me slightly ammend my answer:

"No. Technically, you might be able to come up with some
pointer trickery that will allow you to indirectly call
one function named 'main' from another 'main' in the same
program, but it's bad practice, and you shouldn't do it."

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Apr 6 '08 #17
In article <fc******************************@giganews.com>,
Robbie Hatley <lo******@well.comwrote:
>Walter Roberson wrote:
>Robbie Hatley 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?
>ISO/IEC 9899:1999 (E) section 5.1.2.2.1 :
>5.1.2.2.1 Program startup
It shall be defined with a return type of int and with no
parameters:
or with two parameters (referred to here as argc and argv,
or equivalent;9) or in some other implementation-defined manner.
Your posting to which I replied said "There must be exactly ONE main,
with one of these two signatures" You missed that last part "or in some
other implementation-defined manner", which allows for the
possibility of other signatures.

>Now, you can try to violate that by having functions called
"main" in two different modules
"try"?? I showed source code!

Your amended answer should therefore not have been
"No. You might be able to trick it, but don't do that"; it should
instead have been "Yes, you can trick it -- but don't do that".
--
"Walter is undoubtedly the country's and club's most popular player."
-- vitalfootball.co.uk
Apr 6 '08 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Theo James | last post: by
192 posts views Thread by Kwan Ting | last post: by
92 posts views Thread by Raghavendra R A V, CSS India | last post: by
48 posts views Thread by yezi | last post: by
6 posts views Thread by gamehack | last post: by
6 posts views Thread by fatwallet961 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.