C++ atoi Function Guide
The atoi function in C++ is used to convert a string to an integer.
The function declaration is as follows:
int atoi(const char* str);
The parameter ‘str’ is a pointer to a string that ends with null.
The return value of the function is an integer that represents the converted result.
The function works by reading numeric characters from the parameter string until a non-numeric character is encountered. If it is unable to convert the entire string, the function will return the successfully converted portion.
Here is an example of how it is used:
#include <iostream>
#include <cstdlib>
int main() {
const char* str = "12345";
int num = atoi(str);
std::cout << "Converted number: " << num << std::endl;
return 0;
}
The output is:
Converted number: 12345