Jump to content
  • Advertisement
Sign in to follow this  

std::iostream output decisions

This topic is 3190 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;

  • Sign in to follow this  

    • Advertisement

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!