C++ Tutorial/Data Types/long

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

Cast int to long

#include<iostream.h>
main()
{
  int n = 123;
  for(int i=n;i<=n+9;i++)
  {
    cout<<i;
    cout<<" "<<long(i)*i<<" "<<long(i)*i*i;
    cout<<endl;
  }
  return 0;
}
123 15129 1860867
124 15376 1906624
125 15625 1953125
126 15876 2000376
127 16129 2048383
128 16384 2097152
129 16641 2146689
130 16900 2197000
131 17161 2248091
132 17424 2299968

Output pointer value using cout

#include <iostream>
using std::cout;
using std::endl;
int main() {
  long* pnumber;                        
  long number1 = 55L;
  long number2 = 99L;                   
  pnumber = &number1;                   
  *pnumber += 11;                       
  cout << endl
       << "number1 = "     << number1
       << "   &number1 = " << pnumber
       << endl;
  pnumber = &number2;                   
  number1 = *pnumber * 10;              
  cout << "number1 = "     << number1
       << "   pnumber = "  << pnumber
       << "   *pnumber = " << *pnumber
       << endl;
  return 0;
}
number1 = 66   &number1 = 0x22ff70
number1 = 990   pnumber = 0x22ff6c   *pnumber = 99

pointer to long value

#include <iostream>
using std::cout;
using std::endl;
int main() {
  long number = 50L;
  long* pnumber;
  pnumber = &number;
  cout << endl
       << "The value stored in the variable number is "
       << *pnumber
       << endl;
  return 0;
}
The value stored in the variable number is 50

Use long as function parameter and return type

#include <iostream.h>
long Exp(long x,long n);
main()
{
   long   a,b,c;
   cout << "Enter 2 integers:";
   cin >> a >> b ;
   c = Exp(a,b);
   cout << "\nResult is:" << c << endl;
   return 0;
}
long Exp(long x,long n)
{
       long res=1;
       for (int i=1;i<=n;i++)
          res = res*x;
       return res;
}
Enter 2 integers:1 2
Result is:1