{"id":3021,"date":"2018-03-31T00:01:36","date_gmt":"2018-03-31T07:01:36","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3021"},"modified":"2018-03-24T15:00:28","modified_gmt":"2018-03-24T22:00:28","slug":"the-standard-error-device","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3021","title":{"rendered":"The Standard Error Device"},"content":{"rendered":"<p>Oh, I could have fun with the term &#8220;standard error device.&#8221; It could be any PCs running Windows 8. [rimshot] See? Tech humor! It&#8217;s nerdy, but that&#8217;s not the point of the standard error device with regards to C programming.<br \/>\n<!--more--><br \/>\nIn C, as well as other programming languages, three standard I\/O devices are available:<\/p>\n<p><code>stdin<\/code> for standard input<br \/>\n<code>stdout<\/code> for standard output<br \/>\n<code>stderr<\/code> for error message output<\/p>\n<p>The <code>stdin<\/code> and <code>stdout<\/code> devices are traditionally mapped to the keyboard and screen, respectively. In the olden days, these devices were a teletype. Anyway, input and output from these devices can be redirected. Input can flow from a file, for example, and output can be sent to a file or to another program, where it becomes standard input.<\/p>\n<p>The <code>stderr<\/code> device cannot be redirected. It&#8217;s always mapped to the same gizmo, which on modern computers is the terminal window. This device generates error messages or other text a programmer wants displayed and not sent elsewhere.<\/p>\n<p>When you use any of C standard I\/O library functions, they default to <code>stdin<\/code> and <code>stdout<\/code> for input and output:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"Here is Line 1\\n\");\r\n    printf(\"Here is Line 2\\n\");\r\n    printf(\"Here is Line 3\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code generates three lines of text. The <em>printf()<\/em> statement uses the <code>stdout<\/code> device by default. Here&#8217;s the sample run:<\/p>\n<pre><code>Here is Line 1\r\nHere is Line 2\r\nHere is Line 3<\/code><\/pre>\n<p>This code is identical to the following:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    fprintf(stdout,\"Here is Line 1\\n\");\r\n    fprintf(stdout,\"Here is Line 2\\n\");\r\n    fprintf(stdout,\"Here is Line 3\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this example, the <em>fprintf()<\/em> function replaces <em>printf()<\/em>. It requires a stream argument before the format string, which can be the handle of an open file or one of the standard I\/O devices. In C, <code>stdout<\/code> is accepted as a stream argument, always open for every program you run, as are <code>stdin<\/code> and <code>stderr<\/code>. So in this example, the strings are sent to the &#8220;file&#8221; <code>stdout<\/code>. The code&#8217;s output is the same.<\/p>\n<p>In the next example, I changed the second <em>fprintf()<\/em> statement to use the <code>stderr<\/code> output device instead of <code>stdout<\/code>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    fprintf(stdout,\"Here is Line 1\\n\");\r\n    fprintf(stderr,\"Here is Line 2\\n\");\r\n    fprintf(stdout,\"Here is Line 3\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Again, the output is the same, but unlike <code>stdout<\/code>, <code>stderr<\/code> output cannot be redirected to another device. When I run the code in a terminal window and redirect the output elsewhere, I see only the text generated by the second <em>fprintf()<\/em> statement:<\/p>\n<pre><code>$ .\/a.out > \/dev\/null \r\nHere is Line 2<\/code><\/pre>\n<p>Above, the program (named <code>a.out<\/code>) sent output the <code>\/dev\/null<\/code> device, which is a &#8220;door to nowhere&#8221; in the Unix operating system. (In Windows, use <code>&gt; nul<\/code>.) The first and third <em>fprintf()<\/em> statement&#8217;s output meanders off in to the <code>null<\/code> device, but because the second <em>fprintf()<\/em> statement sends it output to <code>stderr<\/code>, it shows up in the terminal window.<\/p>\n<p>In my code, I use the <code>stderr<\/code> device to output error messages. Especially for programs I write where I know the output is immediately fed into another program, it&#8217;s important that I see those error messages directly. Output to <code>stderr<\/code> guarantees that they show up, but the output isn&#8217;t without its quirks.<\/p>\n<p>Because I\/O in C is buffered, it&#8217;s common that an error message sent to <code>stderr<\/code> appears out of sequence with other text sent to standard output. I tried to replicate this peculiarity with a sample program, but it takes too much code to make it work consistently. Still, it&#8217;s possible for an error message to show up earlier than other text sent to (buffered) standard output. That weird sequencing bugged me, but it&#8217;s something you must understand when you use <code>stderr<\/code> to generate error output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sending errors to the standard error device is useful, but it&#8217;s not without its quirks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3021\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3021","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3021"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3021\/revisions"}],"predecessor-version":[{"id":3037,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3021\/revisions\/3037"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}