简体   繁体   中英

How to convert String to int in C

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?

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.

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

还有sscanf()

balance = atoi(ptr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM