By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

include for command line arguments

P: n/a
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?

--
imalone
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ian Malone wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
Pete Becker wrote:
Ian Malone wrote:

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?


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)

<snip>

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


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.

--
imalone
Jul 23 '05 #3

P: n/a
Ian Malone wrote:
(It also works in C if I leave out
stdio.h, even though I'm supposed to include it)


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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4

P: n/a
On 2005-02-18 09:13:37 -0500, Ian Malone <ib***@cam.ac.uk> said:
Pete Becker wrote:
Ian Malone wrote:

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?


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[i];

}
return 0;
}

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #5

P: n/a
Pete Becker wrote:
Ian Malone wrote:
(It also works in C if I leave out
stdio.h, even though I'm supposed to include it)


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.

--
imalone
Jul 23 '05 #6

P: n/a
On Fri, 18 Feb 2005 12:27:23 +0000, Ian Malone
<ib***@cam.ac.uk> wrote:
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[i][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
Jul 23 '05 #7

P: n/a
Chris Croughton wrote:
On Fri, 18 Feb 2005 12:27:23 +0000, 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.

--
imalone
Jul 23 '05 #8

P: n/a
On Fri, 18 Feb 2005 15:19:54 +0000, Ian Malone
<ib***@cam.ac.uk> wrote:
Chris Croughton wrote:
On Fri, 18 Feb 2005 12:27:23 +0000, 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.


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

Chris C
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.