How to use atol() In C++
In C++, the atol() function translates a string and returns its equivalent integer value.
Syntax:
long int atol (const char * str)
Parameters: The function accepts one mandatory parameter str which is the representation of an integral number.
Below is the C++ program to convert string to long using atol():
C++
// C++ program to convert // string to long using atol() #include <iostream> using namespace std; // Driver code int main() { char s[] = "123456654" ; long int n = atol (s); cout << n; return 0; } |
123456654
C++ Program For String to Long Conversion
In this article, we will learn how to convert strings to long in C++. For this conversion, there are 3 ways as follows:
- Using stol()
- Using stoul()
- Using atol()
Let’s start by discussing each of these methods in detail.
Example:
Input: s1 = “20”
s2 = “30”Output: s1 + s2
long: 50