How to convert string to int type in c

Asked

Viewed 27,466 times

2

I tried using itoa, but it seems that the Uri platform does not accept, someone could give me another way to convert from string to int

Here’s the code I tried

itoa(n1, p1, 10);
  • Tried to use atoi?

  • I didn’t know I had this, function I need to study a little more

1 answer

6


If your idea is to convert string to int, you should use atoi:

char num[10] = "100";
int valor = atoi(num);

detail: in C there are specific functions according to the type of input and output:

  • atof() Conversion of string in float
  • atoi() Conversion of string to int
  • atoll() Conversion of string to long
  • itoa() Conversion of int to string
  • ltoa() Conversion of long to string
  • For C++11 there is the function std::stoi. See this discussion (in English) about the advantages of the latter: https://stackoverflow.com/questions/20583945/what-is-the-difference-between-stdatoi-and-stdstoi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.