473,245 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

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 4991
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Theo James | last post by:
I am a newbie perl programmer and I have an idea for a perl program, but I want to know if it is possible before I attempt to write it. Here is what I need: I have a client, talking to my server...
192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
6
by: gamehack | last post by:
Hello all, I've thought about having to implement 2 functions, say enter_main_loop(func_name); and a function called quit_application(); which terminates the program. The enter_main_loop() can...
1
by: Jonathan | last post by:
I referenced an mdb file to the main db using Tools -> Reference in the VBA Editor of the main mdb. I am able to open and use the forms in the referenced program, via a public sub that resides...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
6
by: fatwallet961 | last post by:
is the main function in python is exact compare to Java main method? all execution start in main which may takes arguments? like the follow example script - def main(argv): ...
3
by: simritsaini1982 | last post by:
Hello.. Can anybody explain that how member functions of a class has direct access to the data members of the class vidout any need to declare them locally in the function body. As in main() we...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.