{"id":5710,"date":"2019-06-20T05:19:19","date_gmt":"2019-06-20T05:19:19","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-iostream\/"},"modified":"2023-06-26T10:06:17","modified_gmt":"2023-06-26T10:06:17","slug":"c-core-guidelines-iostream","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-iostream\/","title":{"rendered":"C++ Core Guidelines: IOstreams"},"content":{"rendered":"<p>When you interact with the outside world, the iostream library is the way to go in C++. As always, you have to keep a few rules in mind. Let me show which rules.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5706\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/horseshoe-bend-1908283_1280.jpg\" alt=\"horseshoe bend 1908283 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/horseshoe-bend-1908283_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/horseshoe-bend-1908283_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/horseshoe-bend-1908283_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/horseshoe-bend-1908283_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<p>The C++ core guidelines give a good overview of iostreams:<code class=\"highlighter-rouge no-highlight\"> \"<em>iostream<\/em><\/code><em>s is a type safe, extensible, formatted and unformatted I\/O library for streaming I\/O. It supports multiple (and user extensible) buffering strategies and multiple locales. It can be used for conventional I\/O, reading and writing to memory (string streams), and <\/em>user-defines<em> extensions, such as streaming across networks (asio: not yet standardized).<\/em>&#8220;<\/p>\n<p>Surprisingly for me, and in contrast to the fact, that iostreams are quite essential and are used by most C++ developers, there are only five rules which deal with them, and these rules do have not much content. Here are they:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-low\">SL.io.1: Use character-level input only when you have to<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-validate\">SL.io.2: When reading, always consider ill-formed input<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-streams\">SL.io.3: Prefer iostreams for I\/O<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-sync\">SL.io.10: Unless you use <code class=\"highlighter-rouge no-highlight\">printf<\/code>-family functions call <code class=\"highlighter-rouge no-highlight\">ios_base::sync_with_stdio(false)<\/code><\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-endl\">SL.io.50: Avoid <code class=\"highlighter-rouge no-highlight\">endl<\/code><\/a><\/li>\n<\/ul>\n<p>I have to add additional information to make a story out of the rules. This is not necessary for the first rule:<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-low\">SL.io.1: Use character-level input only when you have to<\/a><\/h2>\n<p>First, here is a bad example from the guidelines. Using character-level input for more than one character:<\/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: #007788; font-weight: bold;\">char<\/span> c;\r\n<span style=\"color: #007788; font-weight: bold;\">char<\/span> buf[<span style=\"color: #ff6600;\">128<\/span>];\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">while<\/span> (cin.get(c) <span style=\"color: #555555;\">&amp;&amp;<\/span> <span style=\"color: #555555;\">!<\/span>isspace(c) <span style=\"color: #555555;\">&amp;&amp;<\/span> i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">128<\/span>)\r\n    buf[i<span style=\"color: #555555;\">++<\/span>] <span style=\"color: #555555;\">=<\/span> c;\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (i <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">128<\/span>) {\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ... handle too long string ....<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This is a terrible solution for a simple job. My remark does not hold for the right way to do it:<\/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%;\">string s;\r\ns.reserve(<span style=\"color: #ff6600;\">128<\/span>);\r\ncin <span style=\"color: #555555;\">&gt;&gt;<\/span> s;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Presumably, the better way is also the faster way.<\/p>\n<p>The following rule states a no-brainer.<\/p>\n<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-validate\">SL.io.2: When reading, always consider ill-formed input<\/a><\/h2>\n<p>The question is now: How can we deal with bad data? Each stream has a state associated.<\/p>\n<h3>State of the Stream<\/h3>\n<p>Flags represent the state of the stream. The methods for dealing with these flags need the header <span style=\"font-family: courier new, courier;\">&lt;iostream&gt;<\/span>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5707\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StateStream.PNG\" alt=\"StateStream\" width=\"550\" height=\"149\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StateStream.PNG 1448w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StateStream-300x81.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StateStream-1024x277.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/StateStream-768x207.png 768w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p><span style=\"color: inherit; font-size: 18px;\">Examples causing the different states of a stream<\/span><\/p>\n<p><strong><span style=\"font-family: courier new, courier;\">std::ios::eofbit<\/span><\/strong><\/p>\n<ul>\n<li>Reading beyond the last valid character.<\/li>\n<\/ul>\n<p><strong><span style=\"font-family: courier new, courier;\">std::ios::failbit<\/span><\/strong><\/p>\n<ul>\n<li>False formatted reading.<\/li>\n<li>Reading beyond the last valid character.<\/li>\n<li>Opening of a file went wrong.<\/li>\n<\/ul>\n<p><span style=\"font-family: courier new, courier;\"><strong>std::ios::badbit<\/strong><\/span><\/p>\n<ul>\n<li>Size of the stream buffer cannot be adjusted.<\/li>\n<li>Code conversion of the stream buffer went wrong.<\/li>\n<li>A part of the stream threw an exception.<\/li>\n<\/ul>\n<p><span style=\"font-family: courier new, courier;\">stream.fail()<\/span> returns whether <span style=\"font-family: courier new, courier;\">std::ios::failbit<\/span> or <span style=\"font-family: courier new, courier;\">std::ios::badbit<\/span> is set.<\/p>\n<h4>Reading and setting the state of a stream<\/h4>\n<p><strong><span style=\"font-family: courier new, courier;\">stream.clear()<\/span><\/strong><\/p>\n<ul>\n<li>Initializes the flags and sets the stream in the <span style=\"font-family: 'courier new', courier;\">goodbit<\/span> state<\/li>\n<\/ul>\n<p><strong><span style=\"font-family: courier new, courier;\">stream.clear(sta)<\/span><\/strong><\/p>\n<ul>\n<li>Initializes the flags and set the stream into the state <span style=\"font-family: courier new, courier;\">sta.<\/span><\/li>\n<\/ul>\n<p><strong><span style=\"font-family: courier new, courier;\">stream.rdstate()<\/span><\/strong><\/p>\n<ul>\n<li>Returns the current state.<\/li>\n<\/ul>\n<p><strong><span style=\"font-family: courier new, courier;\">stream.setstate(fla)<\/span><\/strong><\/p>\n<ul>\n<li>Sets the additional flag <span style=\"font-family: courier new, courier;\">fla.<\/span><\/li>\n<\/ul>\n<p>Operations on a stream have only an effect if the stream is in the <span style=\"font-family: 'courier new', courier;\">goodbit<\/span> state. If the stream is in the <span style=\"font-family: 'courier new', courier;\">badbit<\/span> state, it can not be reset to the <span style=\"font-family: 'courier new', courier;\">goodbit<\/span> state.<\/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> streamState<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;ios&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::boolalpha <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;\">\"In failbit-state: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::cin<span style=\"color: #555555;\">.<\/span>fail() <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    <span style=\"color: #336666;\">int<\/span> myInt;\r\n    <span style=\"color: #006699; font-weight: bold;\">while<\/span> (std::cin <span style=\"color: #555555;\">&gt;&gt;<\/span> myInt){\r\n        std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Output: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myInt <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl; \r\n        std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #cc3300;\">\"In failbit-state: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::cin<span style=\"color: #555555;\">.<\/span>fail() <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n        std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    }\r\n  \r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #cc3300;\">\"In failbit-state: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::cin<span style=\"color: #555555;\">.<\/span>fail() <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    std::cin<span style=\"color: #555555;\">.<\/span>clear();\r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #cc3300;\">\"In failbit-state: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::cin<span style=\"color: #555555;\">.<\/span>fail() <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>The input of the string <span style=\"font-family: courier new, courier;\">wrongInput<\/span> causes the stream <span style=\"font-family: courier new, courier;\">std::cin<\/span> to be in<span style=\"font-family: courier new, courier;\"> std::ios::failbit<\/span> state. Consequently,&nbsp;<span style=\"font-family: courier new, courier;\">wrongInput<\/span> and <span style=\"font-family: courier new, courier;\">std::cin.fail()<\/span> cannot be displayed. First, you have to set the stream <span style=\"font-family: courier new, courier;\">std::cin<\/span> in the goodbit state.<\/p>\n<p>You can show your output with <span style=\"font-family: courier new, courier;\">printf<\/span> or with iostreams. My tip is obvious.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-streams\">SL.io.3: Prefer iostreams for I\/O<\/a><\/h2>\n<p>The following two program displays two times the equivalent formatted data. First, by using <code>printf<\/code> and format strings; second, by using iostreams and format manipulators.<\/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> printfIostreams<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;cstdio&gt;<\/span>\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    printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"Characters: <\/span><span style=\"color: #aa0000;\">%c<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #aa0000;\">%c<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #cc3300;\">'a'<\/span>, <span style=\"color: #ff6600;\">65<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"Decimals: <\/span><span style=\"color: #aa0000;\">%d<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #aa0000;\">%ld<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #ff6600;\">2011<\/span>, <span style=\"color: #ff6600;\">650000L<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"Preceding with blanks: <\/span><span style=\"color: #aa0000;\">%10d<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #ff6600;\">2011<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"Preceding with zeros: <\/span><span style=\"color: #aa0000;\">%010d<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #ff6600;\">2011<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"Doubles: <\/span><span style=\"color: #aa0000;\">%4.2f<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #aa0000;\">%E<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #ff6600;\">3.1416<\/span>, <span style=\"color: #ff6600;\">3.1416<\/span>);\r\n    printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #aa0000;\">%s<\/span><span style=\"color: #cc3300;\"> <\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #cc3300;\">\"From C to C++\"<\/span>);\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> <span style=\"color: #cc3300;\">\"Characters: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'a'<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  static_cast<span style=\"color: #555555;\">&lt;<\/span>char<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">65<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;  \r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Decimals: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">650000L<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Preceding with blanks: \"<\/span> <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: #ff6600;\">2011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Preceding with zeros: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setfill(<span style=\"color: #cc3300;\">'0'<\/span>) <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: #ff6600;\">20011<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Doubles: \"<\/span> <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;\">3.1416<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> \r\n                             <span style=\"color: #555555;\">&lt;&lt;<\/span> std::setprecision(<span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std::scientific <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #ff6600;\">3.1416<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;\r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"From C to C++\"<\/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>As promised, the same output:<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5708\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreams.png\" alt=\"printfIostreams\" width=\"350\" height=\"359\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreams.png 370w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreams-292x300.png 292w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Okay, but why should you prefer iostreams to <span style=\"font-family: courier new, courier;\">printf?<\/span> There is a subtle but critical difference between <span style=\"font-family: courier new, courier;\">printf<\/span> and iostreams. The format string with <span style=\"font-family: courier new, courier;\">printf<\/span> specifies the type, and the format of the displayed value, and the format manipulator with iostreams specifies only the format. To say it the other way around: <strong>The compiler deduces the correct type automatically in case of an isotream.<\/strong><\/p>\n<p>Let me make my point clear. You get undefined behavior when you have a bad day or are new to C++ and specify the wrong type in a format string.<\/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> printfIostreamsUndefinedBehaviour<span style=\"color: #555555;\">.<\/span>cpp\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">#include &lt;cstdio&gt;<\/span>\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    printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>);\r\n    \r\n    printf(<span style=\"color: #cc3300;\">\"2011: <\/span><span style=\"color: #aa0000;\">%d<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,<span style=\"color: #ff6600;\">2011<\/span>);            \r\n    printf(<span style=\"color: #cc3300;\">\"3.1416: <\/span><span style=\"color: #aa0000;\">%d<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,<span style=\"color: #ff6600;\">3.1416<\/span>);           \r\n    printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\\"<\/span><span style=\"color: #cc3300;\">2011<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\\"<\/span><span style=\"color: #cc3300;\">: <\/span><span style=\"color: #aa0000;\">%d<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,<span style=\"color: #cc3300;\">\"2011\"<\/span>);           \r\n    <span style=\"color: #555555;\">\/\/<\/span> printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #aa0000;\">%s<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>,<span style=\"color: #ff6600;\">2011<\/span>);    <span style=\"color: #555555;\">\/\/<\/span> segmentation fault\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> <span style=\"color: #cc3300;\">\"2011: \"<\/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> <span style=\"color: #cc3300;\">\"3.146: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #ff6600;\">3.1416<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std::endl;   \r\n    std::cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\\"<\/span><span style=\"color: #cc3300;\">2011<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\\"<\/span><span style=\"color: #cc3300;\">: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"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}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This is what undefined behavior looks like on my local PC.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5709\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreamsUndefinedBehaviour.png\" alt=\"printfIostreamsUndefinedBehaviour\" width=\"450\" height=\"243\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreamsUndefinedBehaviour.png 479w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/printfIostreamsUndefinedBehaviour-300x162.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Of course, the compiler usually writes a warning in case of a wrong format string, but you have no guarantee. Additionally, I know what often happens when the deadline is passed. You ignore the warnings and, maybe, will look at it later. Instead of dealing with errors, don&#8217;t make errors in the first place.<\/p>\n<p>The difference between <span style=\"font-family: courier new, courier;\">printf<\/span> and iostreams reminds me of the <a href=\"https:\/\/www.aristeia.com\/Papers\/IEEE_Software_JulAug_2004_revised.htm\">most important design guideline<\/a> from Scott Meyers:<strong> &#8220;<em>Make interfaces easy to use correctly and hard to use incorrectly.<\/em>&#8220;<\/strong><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>I used in the iostream example format specifiers. To simplify your life as a software developer, you should keep a few format manipulators in mind. Let me show in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-format-specifiers-and-optimisation-with-iostreams\">next post<\/a>, which ones.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you interact with the outside world, the iostream library is the way to go in C++. As always, you have to keep a few rules in mind. Let me show which rules.<\/p>\n","protected":false},"author":21,"featured_media":5706,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[447],"class_list":["post-5710","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\/5710","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=5710"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5710\/revisions"}],"predecessor-version":[{"id":6783,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5710\/revisions\/6783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5706"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}