Sign in to follow this  

std::iostream output decisions

This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do the standard output classes from the STL decide what to output given a statement like: std::cout << 65; ? 65 is ASCII for capital A, but it's also an integer. What will be printed, and how does it choose? Also, how does it handle floating-point numbers (i.e., how does it decide when to use scientific notation and when to just display 100.6)? A reference of some kind would be more useful than a straight answer, but either would help.

Share this post


Link to post
Share on other sites
Through function overloading, the compiler selects the correct function depending on the argument's type. Take a look at the overloaded insertion operators for ostream; there are overloads for each basic data type. In addition to those, there are flags that specify how to format that are set with stream manipulators.


cout.setf( ios::hex ); // <-- may not be correct
cout.precision( 5 );
cout << 12.4 << ' ' // Writes a double
<< 12.4f << endl; // Writes a float

// is the same as:
cout << hex << setprecision(5)
<< 12.4 << ' ' // Writes a double
<< 12.4f << endl; // Writes a float

// is the same as:
printf( "%.5f %.5f\n", 12.4f, 12.4 ); // Writes two doubles -- the float is converted to double




By default, all numbers are integers, unless you specify other-wise:

cout << 45 << ' ' // Writes an integer
<< (short)45 << ' ' // Writes an integer
<< (char)45 << ' ' // Writes 'A'
<< static_cast<char>(45) << ' ' // Writes 'A'
<< endl;


  • This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

    If you intended to correct an error in the post then please contact us.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this