tags:

views:

149

answers:

5

I have following thing which i wanted to convert to int.

char *ptr; // this can point to variable length of string

int balance = functionToConverIntoint(ptr)

So is there any such function in C "functionToConverIntoint" which can do this job?

+6  A: 

Check out strtol() and strtoul().

You want to avoid atoi() as it does not have a good way of distinguishing between a string of "0" and an invalid number.

R Samuel Klatchko
strtol has 3 argument where is strtoul has 2 what i should enter in them?
itsaboutcode
Ok thanks i got it, can you tell me the function which can convert string to float?
itsaboutcode
`strtod()` -- check http://linux.die.net/man/3/strtod
pmg
@itsaboutcode: Where did you get this? Both `strtol` and `strtoul` take 3 arguments.
AndreyT
A: 
balance = atoi(ptr)
rzrgenesys187
A: 

Yes. atoi is a basic one with very limited error handling capability; strtol is a better one.

caf
A: 

There's also sscanf().

Artelius
Unfortunately, `sscanf` offers no overflow protection. `strto...` is the only solution in this case.
AndreyT
A: 

strtol()

John Bode