Public Group

# std::iostream output decisions

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

## 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 on other sites
Moved to For Beginners.

##### 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 correctcout.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;

• 22
• 10
• 19
• 15
• 14