reading in command line argument

N

nic977

I am trying to read in couple things from the command line argument, and
one of them suppose to be a integer, but when i try to cast it from a
char * to an integer, it gives strange number...... any idea how should
i do it....



this is the way i do it



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

{

....

int n;

n = (int) argv[1];

....

}
 
J

Joona I Palaste

nic977 said:
I am trying to read in couple things from the command line argument, and
one of them suppose to be a integer, but when i try to cast it from a
char * to an integer, it gives strange number...... any idea how should
i do it....

You're not supposed to cast it to an integer. Look up strtol().
this is the way i do it


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

Undefined behaviour. This should be:
int main(int argc, char *argv[])
n = (int) argv[1];

As I said, this is the wrong way altogether. strtol() is your friend.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
 
A

Andreas Kahari

I always use atol. Is there any difference between them ?

atol(const char *nptr)

is equivalent to

strtol(nptr, (char **)NULL, 10)

.... but atol doesn't affect errno in any way. Also for atol, if
the value in the result can not be represented, the behaviour is
undefined.
 
T

Tom Zych

nic977 said:
I am trying to read in couple things from the command line argument, and
one of them suppose to be a integer, but when i try to cast it from a
char * to an integer, it gives strange number...... any idea how should
i do it....
void main (int argc, char *argv[])

There are so many books out there that will tell you to use void
main. Please ignore them on this point. Your program will return a
value to its calling environment whether you tell it to or not. The
environment may expect this value to be meaningful and use it for
error checking or other purposes. If you use void main, it won't be
meaningful, and things will break.
n = (int) argv[1];

argv[1] (assuming there is one, which you should check) is a pointer
to char - an ordered collection of characters, such as "1234". If
you cast this pointer to int, you'll get some undefined value. You
need to use a conversion function such as atoi(), atol(), or
strtol().
 
A

Andreas Kahari

Tom Zych wrote: said:
Also, according to this manpage I'm reading, strtol isn't ANSI. So
it may not be available on some systems.

I'm sorry to hear that. Please inform the maintainer that it
needs updating. In the standard I'm holding in my hand, it says
it's very much a standard library function defined in <stdlib.h>
(C99, 7.20.1.4).
 
T

Tom Zych

Andreas said:
In fact, it was added for C89.

Hmm, you're right. It's right here in K&R2. And (FWIW) gcc -ansi
-pedantic has no problem with it.

manpage says:

CONFORMING TO
strtol() conforms to SVID 3, BSD 4.3, ISO 9899 (C99)
and POSIX, and strtoll() to ISO 9899 (C99) and POSIX
1003.1-2001.

I just did a little research and found that ISO 9899 = ANSI C. But,
this says ISO9899 (C99), which sounds like they're saying it wasn't
in C89. Misleading, at least. Or maybe not to more experienced
people?
 
D

Dave Thompson

I always use atol. Is there any difference [from strtol]?

atol(const char *nptr)

is equivalent to

strtol(nptr, (char **)NULL, 10)
(Or NULL without the cast, if a prototype declaration is in scope.)
... but atol doesn't affect errno in any way. Also for atol, if
the value in the result can not be represented, the behaviour is
undefined.

Nit: atol is not *required* to set errno, where strtol is for errors;
it *may*, like all library functions that don't have more specific
requirements, not to mention all UB; and *if* implemented using the
equivalence, which is certainly reasonable though not required, it
will set errno (and not do any "bad" UB). That is, portable code
can't rely on it setting errno *or* rely on it not doing so.

- 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,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top