{"id":2077,"date":"2016-08-20T00:01:49","date_gmt":"2016-08-20T07:01:49","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2077"},"modified":"2016-08-13T10:56:02","modified_gmt":"2016-08-13T17:56:02","slug":"basic-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2077","title":{"rendered":"Basic Output"},"content":{"rendered":"<p>The typical first program taught to anyone learning C (or any other programming language) is <em>Hello, world!<\/em> It&#8217;s fun, which sets up false expectations, but it&#8217;s also genius.<br \/>\n<!--more--><br \/>\nWhen that first program uses standard output, it generates results a beginner instantly sees on the screen. That feedback helps boost confidence. It also encourages play because, if you&#8217;re like most people, you next want to change the program&#8217;s output to say something else, perhaps your name or something filthy. At least that&#8217;s how I remember learning to program.<\/p>\n<p>The statement presented in C is:<\/p>\n<p><code>printf(\"Hello, world!\\n\");<\/code><\/p>\n<p>Yet, the <em>printf()<\/em> function is perhaps one of the most complex functions for a beginner to learn. Another function, one that&#8217;s designed to put a string to standard output is:<\/p>\n<p><code>puts(\"Hello, world\");<\/code><\/p>\n<p>This statement also has the benefit of not requiring the <code>\\n<\/code> (newline) escape character, which saves a writer such as myself time because I don&#8217;t have to explain escape characters right away. (Though I do use <em>printf()<\/em> in my first program examine in <em>Beginning Programming with C For Dummies<\/em>.)<\/p>\n<p>Both <em>printf()<\/em> and <em>puts()<\/em> are higher-level output functions. Internally, they rely upon lower level functions that send a single character at a time to standard output. These functions include:<\/p>\n<ul>\n<li><em>putchar()<\/em><\/li>\n<li><em>putc()<\/em><\/li>\n<li><em>fputc()<\/em><\/li>\n<\/ul>\n<p>Of these three, the only &#8220;real&#8221; one is <em>fputc()<\/em>. That&#8217;s because <em>putchar()<\/em> is usually implemented as a macro in the <code>stdio.h<\/code> header file. And the <em>putc()<\/em> function is pretty much identical to <em>fputc()<\/em>, though on some compilers it&#8217;s dedicated to sending a character to standard output. That leaves <em>fputc()<\/em> as the bottom-level character output function. Here&#8217;s the man page format:<\/p>\n<p><code>int fputc(int c, FILE *stream);<\/code><\/p>\n<p>The function deals with <em>int<\/em> variable, but it&#8217;s forgivable to use <em>char<\/em> variables instead.<\/p>\n<p>Anyway, here is code that uses <em>putchar()<\/em> to display a string of text:<\/p>\n<pre class=\"screen\">\r\n#include <stdio.h>\r\n\r\nint main()\r\n{\r\n    char string[] = \"Hello there, beautiful!\\n\";\r\n    int index = 0;\r\n\r\n    while(string[index])\r\n    {\r\n        putchar(string[index]);\r\n        index++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop spins through each character in the <code>string[]<\/code> array. <em>putchar()<\/em> sends the character to standard output. The <code>index<\/code> variable is incremented (Line 11), and the loop repeats until the string&#8217;s terminating character, <code>\\0<\/code> (null character) is encountered.<\/p>\n<p>To use <em>putc()<\/em> instead of <em>putchar()<\/em>, you replace Line 10 with:<\/p>\n<pre><code>        putc(string[index],stdout);<\/code><\/pre>\n<p>On the compiler I&#8217;m using, <em>putc()<\/em> and <em>fputc()<\/em> are the same function, so you could stick an <em>f<\/em> before <em>putc<\/em> (above) and get the same output. The <em>stdout<\/em> constant represents the standard output device, the screen.<\/p>\n<p>The bottom line is that at some point internally, the <em>printf()<\/em> and <em>puts()<\/em> function also use one of the single-character output functions to send their text to standard output. Yet you don&#8217;t have to use those functions, or craft your own string output function, because both <em>printf()<\/em> and <em>puts()<\/em> are available.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you peel away the fancy stuff, C has only one real output function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2077\">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-2077","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\/2077","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=2077"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2077\/revisions"}],"predecessor-version":[{"id":2095,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2077\/revisions\/2095"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}