开发者

How to convert String to int in C

开发者 https://www.devze.com 2022-12-11 12:20 出处:网络
I have following thing which i wanted to convert to int. char *ptr; // this can point to variable length of string

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.


There's also sscanf().


balance = atoi(ptr)
0

精彩评论

暂无评论...
验证码 换一张
取 消