include for command line arguments

I

Ian Malone

Sorry if this is a FAQ, but I had a quick look through the
FAQ Lite and couldn't see it. Googling gives inconsistent
answers.

I'm coming from a C background. In C if I want to use
command line arguments my understanding is I must #include
<stdlib.h>.

Do I need to do a similar thing with C++? I.e., what do
I need to do if I want "int main (int argc, char **argv)"?
I've seen sites which include iostream, but that seems to
be so they can use "cout <<". I've seen sites including
stdlib.h; deprecated at best, misguided at worst. So what
should I do? is there a header file I need to use?
 
P

Pete Becker

Ian said:
I'm coming from a C background. In C if I want to use
command line arguments my understanding is I must #include
<stdlib.h>.

There is no such requirement.
Do I need to do a similar thing with C++? I.e., what do
I need to do if I want "int main (int argc, char **argv)"?

What happened when you tried it?
I've seen sites which include iostream, but that seems to
be so they can use "cout <<".

The header <iostream> provides eight global objects for I/O; std::cout
is one of them.

I've seen sites including
stdlib.h; deprecated at best, misguided at worst.

Irrelevant in fact.

So what
should I do? is there a header file I need to use?

Try it.
 
I

Ian Malone

Pete said:
There is no such requirement.



What happened when you tried it?

It works, but I wanted to be sure it works because it should,
not because it happens to. (It also works in C if I leave out
stdio.h, even though I'm supposed to include it)

So what


Try it.

Thanks for the reply, I was really just checking I was doing
the Right Thing. It seems that, in this regard anyway, I am.
 
C

Chris Croughton

Sorry if this is a FAQ, but I had a quick look through the
FAQ Lite and couldn't see it. Googling gives inconsistent
answers.

I'm coming from a C background. In C if I want to use
command line arguments my understanding is I must #include
<stdlib.h>.

Why? You need to include stdio.h for instance if you want to use I/O,
but the following is a perfectly valid program:

int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; ++i)
{
if (argv[0] == '-')
return 0;
}
return 0;
}

Not that it will do anything useful, but it's perfectly good syntax.
Do I need to do a similar thing with C++? I.e., what do
I need to do if I want "int main (int argc, char **argv)"?

Absolutely nothing.
I've seen sites which include iostream, but that seems to
be so they can use "cout <<". I've seen sites including
stdlib.h; deprecated at best, misguided at worst. So what
should I do? is there a header file I need to use?

No. You need to include headers to call any standard functions (you can
use the C++ versions of the C ones, like cstdlib and cstdio instead of
stdlib.h and stdio.h, if you want access to the C standard functions
which are also part of C++), but declaring main() doesn't use anything
in headers, only built-in language features.

Chris C
 
C

Clark S. Cox III

It works, but I wanted to be sure it works because it should,
not because it happens to.

Don't worry, it works because it's supposed to.
(It also works in C if I leave out
stdio.h, even though I'm supposed to include it)

Including stdio.h (or any other header) is irrelevant as to whether or
not you can get command line arguments in C or C++. The following is a
perfectly valid C and C++ program (though it doesn't do anything):

int main(int argc, char **argv)
{
for(int i=0; i<argc; ++i)
{
const char *arg = argv;

}
return 0;
}
 
I

Ian Malone

Pete said:
There is no such requirement. Not in C++ and not in C.

(Checks) You're quite right, sorry. Somehow I managed
to assume it was part of the standard library. Thanks
for helping me clear this up.
 
I

Ian Malone

<snip>

Thanks to everyone who replied. Sorry to drag this
ng into a discussion of C, but I've certainly found
it informative.
 
C

Chris Croughton

<snip>

Thanks to everyone who replied. Sorry to drag this
ng into a discussion of C, but I've certainly found
it informative.

It's C++ as well as C, so it's on topic. No problem...

Chris C
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top