Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parameters conversion

Status
Not open for further replies.

thomas3711

Programmer
Oct 19, 2004
2
0
0
CZ
Please someone help me convert argv from char/string to int, or float or any other numeric format...I need to write program which will be able to count with numbers inserted as parameters...thanks
 
look at atoi, atol, atof

Typically
Code:
int i = atoi ( argv[1] );

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Be careful: cmd line parameter pointers are in argv[] as is i.e. if you call your app in Windows style
Code:
myexe /1
argv[1] is equal to "/1", not "1".
In that case you may find 1st digit in parameter value, for example:
Code:
static const char digits[] = "0123456789";
...
argv[i]+strcspn(argv[i],digits) /* argv[i] if no digits */
Don't forget: atoi(s) returns 0 if s is not a number...
 
>>atoi(s) returns 0 if s is not a number...
Strictly speaking, the return value is undefined upon an error, not 0. :)
 
Hammer,
alas, atoi() returns an undefined value in case of overflow only (may be because of most of CPUs do not report int overflow condition). The language Standard specifies that zero is returned value if atoi() can't convert its argument to number...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top