Passing args to main( )

E

ern

I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?
 
A

Artie Gold

ern said:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?
There are only two portable signatures (modulo synonyms) for the main()
function in C:

int main(void) /* taking no arguments */

or

int main(int argc, char **argv) /* with arguments */

Anything else is non-standard.

HTH,
--ag
 
V

Vladimir S. Oka

ern said:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?

The only portable way of passing arguments to `main` is declaring it as:

int main(int argc, char *argv[])

and pass things on the command line. Judging by your post, you'd be
easily able to do it.

BTW, `argc` gives you the total number of arguments passed, and `argv`
is an array of pointers to strings containing them. Keep in mind that
program name is always the first argument. How it, as well as how other
arguments look like depends on your particular system. Standard does
not specify this, and hence it's off-topic here. You're better off
asking that part of the question in the group specific to your
environment.

--
BR, Vladimir

"If you've done six impossible things before breakfast, why not round
it off with dinner at Milliway's, the restaurant at the end of the
universe?"
 
J

Jordan Abel

I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?

main(int argc, char *argv[])
 
J

Jordan Abel

Keep in mind that program name is always the first argument.

*cough-OT-execl-path-(char*)NULL-/OT-/cough*

Which is why 5.1.2.2.1 keeps saying "If the value of argc is greater
than zero".
 
D

Default User

ern said:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?

This is not of the two forms of main() required to be supported. Your
textbook should show you the right way to handle arguments. What book
are you using?

At any rate, see the FAQs:

http://c-faq.com/ansi/maindecl.html


Brian
 
D

David Paleino

Jordan Abel ha scritto:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?


main(int argc, char *argv[])

<OT>
Just a note (not trolling, nor joking). Why posting something that
someone else already posted? I mean, what's the reason for replying
"main(int argc, char *argv[])", when this answer was already given by
Artie Gold, and when Vladimir S. Oka gave the full explanation of argc
and argv[]? I mean, your answer is only filling server's bandwidth, it's
not giving any detail missing in the previous answers, come on! I don't
really see the reason of giving an already given answer. It's not a
personal thing to you (Jordan Abel), but a general question: I've seen
threads spreading, and getting bigger and bigger, with lots of replies,
each giving the same responses. Is this normal? Am I strange? Crazy?
What? Maybe I missed for too much time from Usenet and the rules changed?
I repeat, I'm not trolling, nor flaming, nor everything else you can
think of. I'm just exposing my opinion.
</OT>

Regards,
David

P.S.: if you, like anyone else, want, we could open a thread about this.
I know, it's completely OT, but I think it's important for the «health»
of Usenet. It's a matter of behaviours.
 
K

Keith Thompson

Jordan Abel said:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?

main(int argc, char *argv[])

"int", main's return type, is spelled with three letters, not zero.
HTH, HAND.
 
J

Jordan Abel

Just a note (not trolling, nor joking). Why posting something that
someone else already posted?

I didn't see it yet. This is part of a larger issue, see below.
Maybe I missed for too much time from Usenet and the rules changed?

Usenet has always been a distributed, asynchronous medium. This
sometimes has some unfortunate consequences. It becomes most apparent
when a question [like this one] has an easy answer because:

A) everyone's responses look pretty much alike

B) more people respond on average

and

C) people respond quickly.
 
J

Jordan Abel

Jordan Abel said:
I want to pass arguments to the main( ) function of my C program.
Currently, I'm using

main(char * args)

But when I try to print args, I get an unhandled exception error. Is
there a better, safer way to pass args to main (from another [python]
module) ?

main(int argc, char *argv[])

"int", main's return type, is spelled with three letters, not zero.
HTH, HAND.

There are zero letters in an alternate spelling that is accepted, though
marked obsolescent, in c89.
 
R

Richard Heathfield

David Paleino said:
Just a note (not trolling, nor joking). Why posting something that
someone else already posted?

Usenet is not a chat network. Updates are done in unsynchronised batches
from server to server, as and when each server sees fit. Articles that have
already appeared on /your/ server may not yet have appeared on someone
else's server. Therefore that other person might well not have seen the
other response at the time they composed and submitted their own.
 
J

Jordan Abel

David Paleino said:


Usenet is not a chat network.

Funny disanalogy, since many chat networks have the same problem, though
on a scale more likely to be measured in seconds than [potentially]
days. And, indeed, when someone asks an easy question on IRC, easily
half a dozen people will jump to give the exact same answer in some of
the busier channels.
 
R

Richard Heathfield

Jordan Abel said:
And, indeed, when someone asks an easy question on IRC, easily
half a dozen people will jump to give the exact same answer in some of
the busier channels.

To be more specific, the exact same /wrong/ answer. :)
 
S

slebetman

Jordan said:
David Paleino said:


Usenet is not a chat network.

Funny disanalogy, since many chat networks have the same problem, though
on a scale more likely to be measured in seconds than [potentially]
days. And, indeed, when someone asks an easy question on IRC, easily
half a dozen people will jump to give the exact same answer in some of
the busier channels.
Updates are done in unsynchronised batches from server to server, as
and when each server sees fit. Articles that have already appeared on
/your/ server may not yet have appeared on someone else's server.
Therefore that other person might well not have seen the other
response at the time they composed and submitted their own.

<OT>
This is also the reason why you should always quote the context of your
replies on Usenet since there are times when people simply cannot 'see'
the context simply because your reply arrives early.
</OT>
 
J

Jordan Abel

Jordan said:
David Paleino said:

Just a note (not trolling, nor joking). Why posting something that
someone else already posted?

Usenet is not a chat network.

Funny disanalogy, since many chat networks have the same problem, though
on a scale more likely to be measured in seconds than [potentially]
days. And, indeed, when someone asks an easy question on IRC, easily
half a dozen people will jump to give the exact same answer in some of
the busier channels.
Updates are done in unsynchronised batches from server to server, as
and when each server sees fit. Articles that have already appeared on
/your/ server may not yet have appeared on someone else's server.
Therefore that other person might well not have seen the other
response at the time they composed and submitted their own.

<OT>
This is also the reason why you should always quote the context of your
replies on Usenet since there are times when people simply cannot 'see'
the context simply because your reply arrives early.

Fortunately, IRC doesn't have such a failing, because of the
spanning-tree layout of the network. Usenet does not necessarily have
such a layout.
 
D

David Paleino

Richard Heathfield ha scritto:
...

Usenet is not a chat network.

Fully agree :)
Updates are done in unsynchronised batches
from server to server, as and when each server sees fit. Articles that have
already appeared on /your/ server may not yet have appeared on someone
else's server. Therefore that other person might well not have seen the
other response at the time they composed and submitted their own.

Erm... I just didn't think about different syncing times, sorry for
that... Maybe is my server that is just too fast :p (/me is joking,
obviously)

Regards,
David
 
A

Arndt Jonasson

Richard Heathfield said:
David Paleino said:


Usenet is not a chat network. Updates are done in unsynchronised batches
from server to server, as and when each server sees fit. Articles that have
already appeared on /your/ server may not yet have appeared on someone
else's server. Therefore that other person might well not have seen the
other response at the time they composed and submitted their own.

Another possible reason is that people post a reply before they have
read all other replies. Not that I intend to accuse the people in question
here of doing that.
 
D

Dave Thompson

There are zero letters in an alternate spelling that is accepted, though
marked obsolescent, in c89.

Nit: implicit int, and implicit function declaration, were NOT marked
obsolescent in C89/90 but are deleted in C99. Old-style (nonprototype)
declarations and definitions WERE marked obsolescent but NOT deleted.

- David.Thompson1 at worldnet.att.net
 

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
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top