{"id":1710,"date":"2016-01-09T00:01:38","date_gmt":"2016-01-09T08:01:38","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1710"},"modified":"2023-06-20T09:10:59","modified_gmt":"2023-06-20T16:10:59","slug":"the-ungetc-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1710","title":{"rendered":"The <em>ungetc()<\/em> Function"},"content":{"rendered":"<p>The standard C library contains a clutch of functions you use all the time, such as <em>printf()<\/em>, <em>getchar()<\/em>, <em>malloc()<\/em>, <em>time()<\/em>, <em>rand()<\/em>, and others. If you look in the library, you may discover some oddball functions that you&#8217;ve never used. One of those oddball functions might be <em>ungetc()<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>ungetc()<\/em> function has been around since C was first introduced back in the 1970s. Due to this crustiness, you would think it serves some valuable, needed purpose. Even after reading the original K&#038;R book, I can&#8217;t discern why the function was deemed important enough to be necessary. Anyway, it&#8217;s in the library.<\/p>\n<p>The <em>ungetc()<\/em> function takes a single character and shoves it back onto an input stream. It&#8217;s sort of the opposite of the <em>getc()<\/em> function, which reads a single character from an input stream. And keep in mind that <em>ungetc()<\/em> is an input function, not an output function. Here&#8217;s the format:<\/p>\n<pre><code>int ungetc(int c, FILE *stream);<\/code><\/pre>\n<p><em>c<\/em> is a character and <em>stream<\/em> is an open file or standard input (<code>stdin<\/code>). The value returned is the character ungetted or the <code>EOF<\/code> on an error.<\/p>\n<p>Useful examples of how this function would be necessary are rare. Basically, whatever character you <em>unget<\/em> is read from the same stream by the next <em>get<\/em> function. The character you unget isn&#8217;t displayed, and it&#8217;s not saved to a file; remember that <em>ungetc()<\/em> is not an output function.<\/p>\n<p>I crafted this sample code to kinda sorta show how <em>ungetc()<\/em> can work:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    ungetc('\\n',stdin);\r\n    ungetc('o',stdin);\r\n    ungetc('l',stdin);\r\n    ungetc('l',stdin);\r\n    ungetc('e',stdin);\r\n    ungetc('h',stdin);\r\n\r\n    while(putchar(getchar()) != '\\n')\r\n        ;\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 5 through 10 unget a series of characters, stuffing them into the standard input stream.<\/p>\n<p>Line 12&#8217;s <em>while<\/em> loop reads a character from standard input and then displays that character, up until the newline is encountered.<\/p>\n<p>The effect of this code is to spew out a line of text as if it were generated by standard input:<\/p>\n<pre><code>hello<\/code><\/pre>\n<p>Characters are inserted (ungetted?) into the input stream in reverse order. The last character inserted is the first character read.<\/p>\n<p>You can use the <em>ungetc()<\/em> function on an open file, but if you subsequently use a file position indicator manipulation function (<a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1572\"><em>fseek()<\/em><\/a>, <em>fsetpos()<\/em>, or <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1563\"><em>rewind()<\/em><\/a>) the characters inserted into the stream are discarded.<\/p>\n<p>Again, I can&#8217;t find any useful examples of how this function could be necessary, but I&#8217;m sure a need was present or Mr. Kernighan and Mr. Ritchie wouldn&#8217;t have sought to add the function to the standard C library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An odd little booger I&#8217;ve never used, but there must be some reason that it&#8217;s in the C library. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1710\">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-1710","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\/1710","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=1710"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1710\/revisions"}],"predecessor-version":[{"id":5935,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1710\/revisions\/5935"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}