{"id":5724,"date":"2019-06-28T08:04:18","date_gmt":"2019-06-28T08:04:18","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-format-specifiers-and-optimisation-with-iostreams\/"},"modified":"2023-06-26T10:05:54","modified_gmt":"2023-06-26T10:05:54","slug":"c-core-guidelines-format-specifiers-and-optimisation-with-iostreams","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-format-specifiers-and-optimisation-with-iostreams\/","title":{"rendered":"Stuff you should know about In- and Output with Streams"},"content":{"rendered":"<p>Today&#8217;s post is about what you should know about Iostreams. In particular, I write about formatted and unformatted In- and Output.<\/p>\n<p><!--more--><\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5711\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/temple-1717088_1280.jpg\" alt=\"temple 1717088 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/temple-1717088_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/temple-1717088_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/temple-1717088_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/temple-1717088_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h2>Input and Output Functions<\/h2>\n<p>C++ has four predefined stream objects for the convenience of dealing with the keyboard and the monitor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5712\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/PredefinedStreamObject.png\" alt=\"PredefinedStreamObject\" width=\"500\" height=\"131\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/PredefinedStreamObject.png 933w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/PredefinedStreamObject-300x79.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/PredefinedStreamObject-768x202.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The stream objects can be used to write a program that reads from the command line and returns the sum.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555;\">\/\/<\/span> Iostreams<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #336666;\">int<\/span> main(){\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  \r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Type in your numbers(Quit with an arbitrary character): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  \r\n  <span style=\"color: #336666;\">int<\/span> <span style=\"color: #336666;\">sum<\/span>{<span style=\"color: #ff6600;\">0<\/span>};\r\n  <span style=\"color: #336666;\">int<\/span> val;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">while<\/span> ( std::cin <span style=\"color: #555555;\">&gt;&gt;<\/span> val ) <span style=\"color: #336666;\">sum<\/span> <span style=\"color: #555555;\">+=<\/span> val;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Sum: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #336666;\">sum<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  \r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5713\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Iostreams.png\" alt=\"Iostreams\" width=\"450\" height=\"206\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Iostreams.png 695w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Iostreams-300x138.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>The program uses the stream operators <span style=\"font-family: courier new, courier;\">&lt;&lt;<\/span> and<span style=\"font-family: courier new, courier;\"> &gt;&gt;<\/span> and the stream manipulator <span style=\"font-family: courier new, courier;\">std::endl<\/span>.<\/p>\n<ul>\n<li>The insert operator <span style=\"font-family: courier new, courier;\">&lt;&lt;<\/span> pushes characters onto the output stream <span style=\"font-family: courier new, courier;\">std::cout<\/span>.<\/li>\n<li>The extract operator <span style=\"font-family: courier new, courier;\">&gt;&gt;<\/span> pulls the characters from the input stream <span style=\"font-family: courier new, courier;\">std::cin. <\/span><\/li>\n<li>You can build chains of insert or extract operators because both operators return a reference to themselves.<\/li>\n<\/ul>\n<p><span style=\"font-family: courier new, courier;\">std::endl<\/span> is a stream manipulator because it puts a <span style=\"font-family: courier new, courier;\">\u2018\\n\u2019<\/span> character onto <span style=\"font-family: courier new, courier;\">std::cout<\/span> and flushes the output buffer.<\/p>\n<p>Here are the most frequently used stream manipulators.<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5714\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StreamManipulators.png\" alt=\"StreamManipulators\" width=\"500\" height=\"122\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StreamManipulators.png 925w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StreamManipulators-300x73.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StreamManipulators-768x187.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/> Input<\/h3>\n<p>You can read in two ways from the input stream: Formatted with the extractor <span style=\"font-family: courier new, courier;\">&gt;&gt;<\/span> and unformatted with explicit methods.<\/p>\n<\/p>\n<h4>Formatted Input<\/h4>\n<p>The extract operator <span style=\"font-family: courier new, courier;\">&gt;&gt;<\/span><\/p>\n<ul>\n<li>is predefined for all built-in types and strings,<\/li>\n<li>can be implemented for user-defined data types,<\/li>\n<li>can be configured by format specifiers.<\/li>\n<\/ul>\n<p>The following code snippet shows a straightforward way to read two <span style=\"font-family: courier new, courier;\">int<\/span>&#8216;s.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #555555;\">...<\/span>\r\n<span style=\"color: #336666;\">int<\/span> a, b;\r\nstd::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Two natural numbers: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\nstd::cin <span style=\"color: #555555;\">&gt;&gt;<\/span> a <span style=\"color: #555555;\">&gt;&gt;<\/span> b; <span style=\"color: #555555;\">\/\/<\/span> <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">2000<\/span> <span style=\"color: #ff6600;\">11<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nstd::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"a: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> a <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" b: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">std::cin<\/span> ignores by default leading whitespace.<\/p>\n<h4>Unformatted Input<\/h4>\n<p>An input stream supports a few methods for unformatted input.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5715\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/UnformattedInput.png\" alt=\"UnformattedInput\" width=\"600\" height=\"265\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/UnformattedInput.png 1183w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/UnformattedInput-300x133.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/UnformattedInput-1024x453.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/UnformattedInput-768x340.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><strong><span style=\"font-family: courier new, courier;\">std::string<\/span> has a <span style=\"font-family: courier new, courier;\">getline<\/span> function<\/strong><\/li>\n<\/ul>\n<p>The <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/string\/basic_string\/getline\"><span style=\"font-family: courier new, courier;\">getline<\/span> <\/a>function of <span style=\"font-family: courier new, courier;\">std::string<\/span> has a big advantage over the <span style=\"font-family: courier new, courier;\">getline<\/span> function of the <span style=\"font-family: courier new, courier;\">istream.<\/span> The <span style=\"font-family: courier new, courier;\">std::string<\/span> automatically takes care of its memory. On the contrary, you have to reserve the memory for the buffer<span style=\"font-family: courier new, courier;\"> buf<\/span> in the call<span style=\"font-family: courier new, courier;\"> is.get(buf, num)<\/span>. Using the <span style=\"font-family: courier new, courier;\">getline<\/span> function&nbsp; is quite convenient because you can also specify a delimiter:<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555;\">\/\/<\/span> inputUnformatted<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;fstream&gt;<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #336666;\">int<\/span> main(){\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::string line;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Write a line: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::getline(std::cin, line);                       <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">1<\/span>)\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> line <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  \r\n  std::ifstream inputFile(<span style=\"color: #cc3300;\">\"test.txt\"<\/span>); \r\n  <span style=\"color: #006699; font-weight: bold;\">while<\/span> ( std::getline(inputFile, line, <span style=\"color: #cc3300;\">';'<\/span>) ) {      <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">2<\/span>)\r\n      std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> line <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  }\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>First, the program reads in line (1) for<span style=\"font-family: courier new, courier;\"> std::cin<\/span>; second, it reads in line (2) from the file <span style=\"font-family: courier new, courier;\">test.txt<\/span>.<\/p>\n<p>For simplicity reasons, the code does no error handling. You can read the error handling details in my last post:&nbsp; <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-iostream\">C++ Core Guidelines: iostreams.<\/a> The file <span style=\"font-family: courier new, courier;\">test.txt<\/span> contains numbers, which are separated by <span style=\"font-family: courier new, courier;\">&#8220;;&#8221;<\/span>.&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5716\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/inputUnformatted.png\" alt=\"inputUnformatted\" width=\"350\" height=\"345\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/inputUnformatted.png 513w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/inputUnformatted-300x295.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<h3>Output<\/h3>\n<p>As promised in my last post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-iostream\">C++ Core Guidelines: iostreams<\/a>, here are the format specifiers for iostreams; you should know, or at least know, where to find them.<\/p>\n<h4>Important Format Specifiers<\/h4>\n<p>I often hear students who are experienced C++ programmers in my classes complain that arithmetic in C++ is not precise enough. The reason is mostly not C++ but the default format specifiers for the Iostreams. Let&#8217;s see what you should know:<\/p>\n<p>First of all. You can use manipulators or flags that specify the format.<\/p>\n<h5><strong>Manipulators and Flags<\/strong><\/h5>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555;\">\/\/<\/span> formatSpecifier<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #336666;\">int<\/span> main(){\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  <span style=\"color: #336666;\">int<\/span> num{<span style=\"color: #ff6600;\">2011<\/span>};\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"num: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> num <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n  std::cout<span style=\"color: #555555;\">.<\/span>setf(std::ios::<span style=\"color: #336666;\">hex<\/span>, std::ios::basefield);     <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">1<\/span>)\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"hex: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> num <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout<span style=\"color: #555555;\">.<\/span>setf(std::ios::dec, std::ios::basefield);     <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">1<\/span>)\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"dec: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> num <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::<span style=\"color: #336666;\">hex<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"hex: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> num <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;   <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">2<\/span>)\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::dec <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"dec: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> num <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;   <span style=\"color: #555555;\">\/\/<\/span> (<span style=\"color: #ff6600;\">2<\/span>)\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Lines (1) use flags and lines (2) manipulators to format the output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5717\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifier.png\" alt=\"formatSpecifier\" width=\"300\" height=\"229\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifier.png 371w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifier-300x229.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>From the readability and maintainability point of view, I strongly prefer manipulators.<\/p>\n<h5><strong>Manipulators for the Iostreams<\/strong><\/h5>\n<p>Okay, let me start with the essential manipulators.<\/p>\n<p>The following tables present the relevant format specifiers. The format specifiers are sticky except for the field width, which is reset after each application.<br \/>The manipulators without arguments need the header <span style=\"font-family: courier new, courier;\">&lt;iostream&gt;,<\/span> and those with arguments need the header<span style=\"font-family: courier new, courier;\"> &lt;iomanip&gt;<\/span>.<\/p>\n<ul>\n<li><strong>Boolean Values<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5718\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/BooleanValue.png\" alt=\"BooleanValue\" width=\"600\" height=\"108\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/BooleanValue.png 950w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/BooleanValue-300x54.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/BooleanValue-768x139.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><strong>Field With and Fill Characters<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5719\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FieldWithAndFillCharacters.png\" alt=\"FieldWithAndFillCharacters\" width=\"500\" height=\"100\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FieldWithAndFillCharacters.png 954w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FieldWithAndFillCharacters-300x60.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FieldWithAndFillCharacters-768x154.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<ul>\n<li><strong>Alignment of Text<\/strong><\/li>\n<\/ul>\n<h4><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5720\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Alignment.png\" alt=\"Alignment\" width=\"600\" height=\"114\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Alignment.png 1147w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Alignment-300x57.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Alignment-1024x194.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/Alignment-768x145.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/h4>\n<ul>\n<li><strong>Positive Signs and Upper\/Lower Case<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5721\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers.png\" alt=\"FloatingPointNumbers\" width=\"600\" height=\"214\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers.png 1142w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-300x107.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-1024x365.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-768x274.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><strong>Numeric Base<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5722\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/NumericBase.png\" alt=\"NumericBase\" width=\"600\" height=\"158\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/NumericBase.png 1148w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/NumericBase-300x79.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/NumericBase-1024x269.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/NumericBase-768x202.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><strong>Floating Point Numbers<\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5721\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers.png\" alt=\"FloatingPointNumbers\" width=\"600\" height=\"214\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers.png 1142w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-300x107.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-1024x365.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/FloatingPointNumbers-768x274.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;There are special rules for floating-point numbers:<\/p>\n<ul>\n<li>The number of significant digits (digits after the comma) is, by default, 6.<\/li>\n<li>If the number of significant digits is not big enough, the number is displayed in scientific notation.<\/li>\n<li>Leading and trailing zeros are not displayed.<\/li>\n<li>If possible, the decimal point is not displayed.<\/li>\n<\/ul>\n<p>&nbsp;After so much theory, here are the format specifiers in action.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555;\">\/\/<\/span> formatSpecifierOutput<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #336666;\">int<\/span> main(){\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::setw, std::setfill and std::left, right and internal: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout<span style=\"color: #555555;\">.<\/span>fill(<span style=\"color: #cc3300;\">'#'<\/span>);\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">12345<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setw(<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">12345<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setw(<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std::left <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">12345<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setw(<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std::right <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">12345<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setw(<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std::internal <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">12345<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::showpos:\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::showpos <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::noshowpos <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::uppercase: \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">12345678.9<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::uppercase <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">12345678.9<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::nouppercase <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::showbase and std::oct, dec and hex: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::<span style=\"color: #336666;\">oct<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::<span style=\"color: #336666;\">hex<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::showbase;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::dec <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::<span style=\"color: #336666;\">oct<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::<span style=\"color: #336666;\">hex<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::dec <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::setprecision, std::fixed and std::scientific: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::fixed <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">4<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">7<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">8<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">9<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::scientific <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">4<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">7<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">8<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">9<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">123.456789<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n  std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;The output should be sufficient to explain the program <span style=\"font-family: courier new, courier;\">formatSpecifierOutput.cpp<\/span>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5723\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifierOutput.png\" alt=\"formatSpecifierOutput\" width=\"500\" height=\"749\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifierOutput.png 695w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifierOutput-200x300.png 200w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/formatSpecifierOutput-684x1024.png 684w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>When you synchronize too much, you lose. In the case of the Iostreams, you will lose performance. I will show you the numbers in my<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-improved-performance-with-iostreams\"> next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s post is about what you should know about Iostreams. In particular, I write about formatted and unformatted In- and Output.<\/p>\n","protected":false},"author":21,"featured_media":5711,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[447],"class_list":["post-5724","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-in-output"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5724"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5724\/revisions"}],"predecessor-version":[{"id":6782,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5724\/revisions\/6782"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5711"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}