"1234" ==> 1234 (char * ==> int)

Yiannis Papelis yiannis at ccad.uiowa.edu
Fri Dec 14 03:21:33 AEST 1990


In article <1990Dec12.215359.5378 at cs.utk.edu> lape at .cs.utk.edu
   (Bryon S. Lape) writes:

>	   I need to know how to convert a string of numbers into an int.
>For example, "1234" ==> 1234.  I am interested in all theories, so please
>send whatever you know or think.  Also, please e-mail the responses.
>

I tried to mail this but it bounced back so:
	atoi("1234") will work like it has been pointed out.
The only problem with that is that you don't get any error checking other
than a return value of 0 in which case you don't know if you parsed
zero or there was an error.  On the other hand

int rcode, num;
char *s;	/* the string to parse */
	rcode = sscanf(s, "%d", &num);

will work and give back a reliable error code (rcode==1 -> conversion OK
rcode == 0 -> no conversion.


-- 
Yiannis E. Papelis      --------      Electrical & Computer Engineering
yiannis at eng.uiowa.edu   --------      University of Iowa



More information about the Comp.std.c mailing list