Using strtol: convert string to long : Long « Data Type « C++






Using strtol: convert string to long

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
   long x;
   char *string = "-1234567abc", *remainderPtr;

   x = strtol( string, &remainderPtr, 0 );
   cout << "The original string is \"" << string
        << "\"\nThe converted value is " << x
        << "\nThe remainder of the original string is \""
        << remainderPtr
        << "\"\nThe converted value plus 567 is " 
        << x + 567 << endl;
   return 0;
}
  
    
  








Related examples in the same category

1.cout for long intcout for long int
2.Using atol: convert string to long
3.Using strtoul: convert string to unsigned long