{"id":54,"date":"2013-05-18T00:01:16","date_gmt":"2013-05-18T08:01:16","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=54"},"modified":"2025-01-05T21:35:18","modified_gmt":"2025-01-06T05:35:18","slug":"error-message-to-stdout","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=54","title":{"rendered":"Error Message to <em>stdout<\/em>"},"content":{"rendered":"<p>The three standard I\/O devices are <code>stdin<\/code>, <code>stdout<\/code>, and <code>stderr<\/code>. They represent the devices for standard input, standard output, and standard error messages. While you might see plenty of examples of <code>stdin<\/code> and <code>stdout<\/code>, examples of <code>stderr<\/code> are less common, although they don&#8217;t have to be.<br \/>\n<!--more--><br \/>\nInternally, the three standard devices are aliased to specific file handles recognized by the operating system. They are typically 0, 1, 2 for <code>stdin<\/code>, <code>stdout<\/code>, and <code>stderr<\/code>, respectively. They could also be file handles 1, 2, 3. Regardless, the names are aliased and you should use the aliases, which are defined in <code>stdio.h<\/code>.<\/p>\n<p><code>stdin<\/code> is the standard input device, the keyboard.<\/p>\n<p><code>stdout<\/code> is the standard output device, the display. Years ago it would have been a printer or a teletype, which brings up the interesting point about both <code>stdin<\/code> and <code>stdout<\/code>: They can be reassigned.<\/p>\n<p>I\/O redirection commands are used at the command prompt to mess with standard input and output. For example, you can send output to a file or fetch input from some other device. The operating system handles the specifics. As programmer, you mustn&#8217;t make assumptions when you code about where I\/O may come or go.<\/p>\n<p>That is, unless you use the <code>stderr<\/code> device.<\/p>\n<p><code>stderr<\/code> is an output device, similar to <code>stdout<\/code>. In fact, it&#8217;s pretty much the same as <code>stdout<\/code>, but unlike that device the output sent to <code>stderr<\/code> cannot be redirected. It always goes to the standard output device, which these days is the display.<\/p>\n<p>Because it can&#8217;t be redirected, the <code>stderr<\/code> device is ideal for displaying errors and other messages that must be seen. Use it in your code for such messages, as shown in the following code.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    FILE *fh;\r\n\r\n    fh = fopen(\"blorfus\",\"r\");\r\n    if( fh == NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to open that file!\\n\");\r\n        return;\r\n    }\r\n    fclose(fh);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the above listing, the error message is sent to the <code>stdout<\/code> device by the <em>fprintf()<\/em> function at Line 10. That message <em>always<\/em> goes to the display (or standard output device) regardless of how badly the program is mangled by I\/O redirection.<\/p>\n<p>I&#8217;ve not heard of an input device similar to <code>stderr<\/code>, nor can I reason why such a thing would be needed. I can also strongly recommend that you don&#8217;t use <code>stderr<\/code> to display regular messages. By doing so you would make your program less useful to others, users who rely on I\/O redirection to run their computer systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While you might see plenty of examples of <code>stdin<\/code> and <code>stdout<\/code>, examples of <code>stderr<\/code> are less common, although they don&#8217;t have to be. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=54\">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-54","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\/54","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=54"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":6810,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/6810"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}