C++/Data Type/Long

Материал из C\C++ эксперт
Перейти к: навигация, поиск

cout for long int

 
#include <iostream>
using namespace std;
int main()
{
  int i = 10;
  long l = 1000000;
  double d = -0.0009;
  cout << i << " " << l << " " << d;
  cout << endl;
  return 0;
}


Using atol: convert string to long

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
   long l = atol( "1000000" );
   cout << "The string \"1000000\" converted to long is " << l
        << "\nThe converted value divided by 2 is " << l / 2 
        << endl;
   return 0;
}


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;
}


Using strtoul: convert string to unsigned long

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
   unsigned long x;
   char *string = "1234567abc", *remainderPtr;
   x = strtoul( 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 minus 567 is " 
        << x - 567 << endl;
   return 0;
}