multiple mains()

P

pranav.choudhary

I hope the question is not silly.
1. Can there me more than one main in a C program.
2. Can main take more arguments thac argc and argv. If yes then whar
are they meant for??
 
P

pemo

I hope the question is not silly.
1. Can there me more than one main in a C program.
2. Can main take more arguments thac argc and argv. If yes then whar
are they meant for??

1. Nope, main() is a function [albeit the one that's called to start a
program off], and you can only have one function called <whatever> in a C
program - else, how would a call to <whatever> be resolved?

2. I've seen some compilers/environments where a third argument is allowed,
typical called something like char * env[] - it allows one to access
environment variables. However, standard C says that main should either be
int main(void), or int main(int, char **).
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I hope the question is not silly.
1. Can there me more than one main in a C program.

No. There can be only one main() function in a program.
2. Can main take more arguments thac argc and argv.

No, yes, and maybe.

No. main() is defined to, in a hosted environment, only have two
arguments: int argc, and char **argv . The local declaration of main()
can either be
int main(void)
or
int main(int argc, char **argv)
(or equivalent)

However, some operating environments take some latitude with the
arguments passed to main(). Some Unixish environments, for instance,
include a third argument (char **envp) to provide access to commandline
arguments.
If yes then whar are they meant for??

What ever the implementation/environment dictates. See the relevant
documentation for your C compiler and execution environment for details.




- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD/wRAagVFX4UWr64RAnfhAKDQauBYPWgv6t/TosA2Ln3MZmSAagCffORm
HgoHT+iEBdbblIVp1CMw3EQ=
=q4Nu
-----END PGP SIGNATURE-----
 
E

Eric Sosman

I hope the question is not silly.
1. Can there me more than one main in a C program.

Yes, but at most one can have external linkage and
thus be "the" main() function, the one where program
execution begins.
2. Can main take more arguments thac argc and argv. If yes then whar
are they meant for??

Every hosted C implementation must support the two
forms of "the" main() function required by the Standard:
one form with no arguments and one with two. A given C
implementation may also support other forms if it chooses;
what those other forms are and what they mean is up to
the implementation that chose to support them.
 
P

pemo

Eric said:
Yes, but at most one can have external linkage and
thus be "the" main() function, the one where program
execution begins.

<snip>

True!

So, something like this:

file1.c

void foo(void);

int main(void)
{
foo();

return 0;
}

file2.c

#include <stdio.h>

static void main(void)
{
puts("In second main()");

}

void foo(void)
{
main();
}
 
P

pranav.choudhary

Eric,
Can you plese elaborate, or get me some links where i can learn more
about the first question i had asked.
thanx
pranav
 
C

CBFalconer

Can you plese elaborate, or get me some links where i can learn
more about the first question i had asked.

I see no first question, nor anything to elaborate, here. Perhaps
if you had had the courtesy to include context I would. If the
failure is due to ignorance, rather than rudeness, you can repair
that by reading the following sig and the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Lew Pitcher said:
However, some operating environments take some latitude with the
arguments passed to main(). Some Unixish environments, for instance,
include a third argument (char **envp) to provide access to commandline
arguments.

No, envp provides access to environment variables.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Keith said:
No, envp provides access to environment variables.

:-(

My bad. Brain f*rt or finger check or something must have caught me. I
/meant/ to say what you said.


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD/21aagVFX4UWr64RAiDyAKCBRQrqOTqMyhA6AolLg+xJkyCbbACg0JG/
WBCGqVESGtLts6qsqIrfZKc=
=CUcB
-----END PGP SIGNATURE-----
 
K

Keith Thompson

I hope the question is not silly.
1. Can there me more than one main in a C program.
[...]

You can have multiple *static* functions with the same name in a
single program, as long as each is in a separate translation unit.

I don't know whether you can do this with main(). It may be
implementation-dependent. But frankly, I don't particularly care
whether you can have multiple static main() functions. Even if it's
legal, it would be confusing, and the confusion could easily be
avoided by calling the function something other than "main".

Is there some problem that you think would be solved by having
multiple functions called main? If so, there's almost certainly a
better way to solve it. What are you trying to do?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top