Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!ns-mx!ccad.uiowa.edu!yiannis From: yiannis@ccad.uiowa.edu (Yiannis Papelis) Newsgroups: comp.std.c Subject: Re: "1234" ==> 1234 (char * ==> int) Message-ID: <1990Dec13.162133.19562@ccad.uiowa.edu> Date: 13 Dec 90 16:21:33 GMT References: <1990Dec12.215359.5378@cs.utk.edu> Organization: CAD-Research, U. of Iowa, Iowa City, Iowa Lines: 25 In article <1990Dec12.215359.5378@cs.utk.edu> lape@.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@eng.uiowa.edu -------- University of Iowa