vendredi 19 mars 2021

Default Precision of double in C++ [duplicate]

I have been learning CPP lately, and I came across this question:

Consider the below program:

#include <iostream>
using namespace std;
class Hall
{
 public:
 double cost;
};
int main()
{
 Hall hall;
 hall.cost=10000.50;
 cout<<hall.cost;
 return 0;
}
What will be the output of above coding? 

The answer to this question is 10000.5, however, I couldn't understand why is it so ? In my opinion, answer should be either 10000.500000 as double has precision upto 6 decimal digits or it should print 10000.50 as it is the value assigned to hall.cost.

Please explain.

Aucun commentaire:

Enregistrer un commentaire