Using strtol to convert string to long integer. - C++ Data Type

C++ examples for Data Type:int

Description

Using strtol to convert string to long integer.

Demo Code

#include <iostream> 
#include <cstdlib> // strtol prototype 
using namespace std; 

int main() //w w  w. ja  v  a  2 s. c om
{ 
    long x; 
    const char *string1 = "-1234567abc"; 
    char *remainderPtr; 

    x = strtol( string1, &remainderPtr, 0 ); // convert characters to long 

    cout << "The original string is \"" << string1 
        << "\"\nThe converted value is " << x 
        << "\nThe remainder of the original string is \"" << remainderPtr 
        << "\"\nThe converted value plus 567 is " << x + 567 << endl; 
}

Result


Related Tutorials