473,413 Members | 1,718 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,413 software developers and data experts.

multiple mains()

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??

Feb 24 '06 #1
9 1906
pr**************@gmail.com wrote:
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 **).

--
==============
*Not a pedant*
==============
Feb 24 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pr**************@gmail.com wrote:
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-----
Feb 24 '06 #3
pr**************@gmail.com wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 24 '06 #4
Eric Sosman wrote:
pr**************@gmail.com wrote:
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.


<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();
}
--
==============
*Not a pedant*
==============
Feb 24 '06 #5
Eric,
Can you plese elaborate, or get me some links where i can learn more
about the first question i had asked.
thanx
pranav

Feb 24 '06 #6
"pr**************@gmail.com" wrote:

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/>
Feb 24 '06 #7
Lew Pitcher <Le*********@tdsecurities.com> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 24 '06 #8
"pr**************@gmail.com" <pr**************@gmail.com> writes:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 24 '06 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Keith Thompson wrote:
Lew Pitcher <Le*********@tdsecurities.com> writes:
[...]
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.


:-(

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+xJkyCbbAC g0JG/
WBCGqVESGtLts6qsqIrfZKc=
=CUcB
-----END PGP SIGNATURE-----
Feb 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rolf Wester | last post by:
Hi, I have a form with a select element with multiple="true". When using the GET method (I suppose the same happens with the POST method) I can seen that the form sends channels=CH1&channels=CH2...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
2
by: Gary Feldman | last post by:
I didn't get any good answers to my previous question about unit testing, so let me rephrase the question: As near as I can see, it's possible to add arbitrary configurations to a C# project in...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
5
by: Victor | last post by:
In his book "Programming Microsoft Windows with C#", page 42, Petzold deals with the case of a project with multiple Mains. He explains that when compiling in the command line the argument /main...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
11
by: a | last post by:
Don't know why this compilation generated, I'm sure no 2 mains exist: make votei gcc vote.c datamanager.c -o votei vote.c -L. -lhungarian vote.c: In function `main': vote.c:254: warning:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.