What is strtol() Function in C++
The strtol() function is a built-in function in C++ that converts a string into a long integer value. It accepts a string as data and transforms it to an integer value with the specified base. In addition, the method returns a link or pointer to the initial incorrect character within the string.
Syntax of strtol() Function
The syntax of the strtol() function in C++ is as follows:
Parameters of strtol() Function
The strtol() function has three parameters:
- s: A pointer to the string that is to be converted into a long integer value.
- end: A pointer to the first invalid character in the string, or to the string itself if no invalid characters are found.
- base: The base of the number that is to be converted. The base can be between 2 and 36, and if it is set to 0, the function determines the base based on the prefix of the input string.
Return Type of strtol() Function
The strtol() function returns a long integer value that is converted from the input string. If no conversion occurs, it returns 0.
Header-File in C++ to Use strtol() Function in C++
Before using the strtol() function in your C++ code, you need to define the #include <cstdlib> header file.
Examples of strtol() Function in C++
The following are some examples of using strtol() function in C++.
Example 1: Program to Convert a String into Long Integers using Decimal Base
With strtol() function, it’s pretty simple to convert a string into a long integer using the decimal base. The code of C++ needed in such a situation is shown below.
#include <cstdlib>
using namespace std;
int main() {
char str[] = "12345678";
char* endptr;
long num = strtol(str, &endptr, 10);
if (endptr == str) {
cout<<"Invalid input"<<endl;
} else {
cout<<"The number is: "<<num;
}
return 0;
}
The above program declares a character array named str, which contains the string “12345678”. A pointer named endptr is used to store the address of the first character in the string that is not part of the converted number. The strtol() function is then called, with the argument’s str, &endptr, and 10. The first argument is the string to be converted, the second argument is the address of the pointer that will be updated with the end position of the string, and the third argument is the base for the conversion, which is decimal in this case.
The program then checks whether the endptr is equal to the start of the string. If it is, it means that the conversion was not successful, and an error message is printed. Otherwise, the program prints the converted number.
Output
Example 2: Program to Convert Strings into Long Integers with 0 Bases
If we set the base argument of the strtol() function to 0, the function will automatically detect the base from the input string. This makes it easier to convert strings of different bases without having to explicitly specify the base. For example, if the string begins with “0x”, it is automatically detected as a hexadecimal number.
By using the strtol() function to convert strings into long integers with 0 bases, we can ensure that the input string is valid and can be properly converted into a long integer. This can be helpful in many kinds of implementations, such as reading numeric values from user input or analyzing information from text documents.
The program given below is well according to the previously stated situation.
#include <cstdlib>
using namespace std;
int main() {
char str[] = "0xA0B0C0";
char* endptr;
long num = strtol(str, &endptr, 0);
if (endptr == str) {
cout<<"Invalid input"<<endl;
} else {
cout<<"The number is: "<<num;
}
return 0;
}
The above program declares a character array named str, which contains the string “0xA0B0C0”. The ‘0x’ prefix indicates that the string is in hexadecimal format. A pointer named endptr is also declared, which will be used to store the address of the first character in the string that is not part of the converted number. The strtol() function is then called, with the argument’s str, &endptr, and 0. The base for the conversion, which is set to 0 automatically detects the base from the input string.
The program then checks whether the endptr is equal to the start of the string. If it is, it means that the conversion was not successful, and an error message is printed. Otherwise, the program prints the converted number.
Output
Conclusion
In C++, strtol() function is used for the conversion of a string into a long integer value. Following the above guidelines, you will be able to learn the use of strtol() function in C++ since you will find different examples that will help you in understanding the basics of this function.