Kane B
Kane B

Reputation: 21

why does this c++ static cast code produce and int not a double

Im new to C++ and i'm not sure why the output for this code is 8 and not 8.25?

can someone explain why this code outputs an int not a double?

Thanks :)

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


int main()
{

double x = 8.25;
    int y;
    y = x;

    double z = static_cast<double>(y);
    std::cout << z << std::endl;
return 0;
}

Upvotes: 0

Views: 59

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The data is converted to the integer 8 in the statement y = x.

A static_cast cannot recover the lost ".25" after throwing it away by converting to int.

Upvotes: 5

Related Questions