Re: Is it possible to have two main functions in a c program?
jacob navia <jacob@nospam.c omwrites:
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
Re: Is it possible to have two main functions in a c program?
Ben Pfaff wrote:
jacob navia <jacob@nospam.c omwrites:
>
>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
Re: Is it possible to have two main functions in a c program?
jacob navia <jacob@nospam.c omwrites:
Ben Pfaff wrote:
>jacob navia <jacob@nospam.c omwrites:
>>
>>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);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,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)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Re: Is it possible to have two main functions in a c program?
In article <ft36q8$v8k$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
>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
Re: Is it possible to have two main functions in a c program?
"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
>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
>>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.
Re: Is it possible to have two main functions in a c program?
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.
Re: Is it possible to have two main functions in a c program?
In article <26bc7$47f542f5 $541dfcd3$17366 @cache3.tilbu1. nb.home.nl>,
Harald van Dijk <truedfx@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].
Comment