Click here to Skip to main content
15,665,718 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
I need to read two integers from the command line via argc and *argv[] for my homework. How can I achieve this? As you know, the parameters are strings and I have to convert them to integers in order to use it. Thank you.
Updated 19-Jan-11 14:16pm
Yusuf 19-Jan-11 21:05pm    
Why is this down-voted. OP clearly stated he need help with his HW, not do my HW.

function atoi[^] will convert string to integer.
Share this answer
Nish Nishant 19-Jan-11 20:18pm    
Voted 5, proposed as answer.
HimanshuJoshi 19-Jan-11 20:19pm    
Thanks Nish.
Sergey Alexandrovich Kryukov 19-Jan-11 20:35pm    
A 5.
HimanshuJoshi 19-Jan-11 20:39pm    
Thanks SA
atoi is perfect in this case, however there are other functions which you may find usefull in the future for similar tasks

strtol - "string to long"
strtoul - "string to unsigned long"
strtod - "string to double"

These functions take a char **_EndPtr parameter which on return holds the position that the new string pointer after the number processed. This is helpful if you have a number in a string.

strtol and strtoul take the radix as an additional parameter to specify the number base of the string so you can convert binary, octal, decimal, hexadecimal or any other base string to its integer equivalent

check out their pages on MSDN: atoi[^], strtol[^], stroul[^], strtod[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900