How to convert String to int in C

前端 未结 4 1464
萌比男神i
萌比男神i 2020-12-04 03:20

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         


        
相关标签:
4条回答
  • 2020-12-04 04:06
    balance = atoi(ptr)
    
    0 讨论(0)
  • 2020-12-04 04:08

    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.

    0 讨论(0)
  • 2020-12-04 04:13

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

    0 讨论(0)
  • 2020-12-04 04:17

    There's also sscanf().

    0 讨论(0)
提交回复
热议问题