Saturday, January 7, 2012

C/C++ Casting and static_cast

Example:
C/C++ Casting and static_cast
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 cout << "casting: " << endl << endl;

 float f1 = 0.3345;
 float f2 = 12.41;

 float fa = f1 * f2;
 int i1 = f1 * f2;
 int i2 = (int)f1 * (int)f2;
 int i3 = (int)(f1 * f2);
 int i4 = static_cast<int>(f1 * f2);
 int i5 = static_cast<int>(f1) * static_cast<int>(f2);

 cout << "fa = " << fa << endl;
 cout << "i1 = " << i1 << endl;
 cout << "i2 = " << i2 << endl;
 cout << "i3 = " << i3 << endl;
 cout << "i4 = " << i4 << endl;
 cout << "i5 = " << i5 << endl;

 system("PAUSE");
 return 0;
}


No comments:

Post a Comment