The static_cast C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Casting the integer type to double using C++ static_cast type casting

To show: The static_cast usage in C++ program for type casting (conversion)

 

 

#include <iostream>

using namespace std;

 

int main(void)

{

int sum = 1000;

int count = 21;

double average1 = sum/count;

 

cout<<"Before conversion = "<<average1<<endl;

double average2 = static_cast<double>(sum)/count;

cout<<"After conversion = "<<average2<<endl;

return 0;

}

 

Output example:

 

Before conversion = 47

After conversion = 47.619

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index