{"id":7339,"date":"2026-01-10T00:01:48","date_gmt":"2026-01-10T08:01:48","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7339"},"modified":"2026-01-17T09:51:05","modified_gmt":"2026-01-17T17:51:05","slug":"cooked-or-raw","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7339","title":{"rendered":"Cooked or Raw?"},"content":{"rendered":"<p>When the waitress asks how I like my eggs, I answer, &#8220;Cooked.&#8221;<\/p>\n<p>O, how we all laugh . . .<\/p>\n<p>The humor here is that it&#8217;s assumed you desire your eggs to be cooked and the waitress&#8217;s question relates to the fashion by which the eggs are to be cooked: fried, over-easy, sunny-side-up, scrambled, poached, and on and on. But the question of cooked or raw also applies to standard input for a computer terminal.<br \/>\n<!--more--><br \/>\nJust like ordering eggs in a diner, standard input for a computer terminal is assumed to be in the &#8220;cooked&#8221; mode. This setting doesn&#8217;t imply that the text generated is warm or tasty. No, it means that the characters input have certain effects applied, specifically the control characters. As cooked mode is the default terminal behavior, you probably don&#8217;t even think about it.<\/p>\n<p>For example, when your program fetches standard input, the user can backspace and erase characters before pressing the Enter key. The backspace character (ASCII code 8) doesn&#8217;t feed into standard input; the ^H (control-H) character isn&#8217;t in the queue. In fact, even the Enter key press is assumed to signal the end of input.<\/p>\n<p>The following code reads standard input and outputs the text input.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_01_10-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_01_10-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char  buffer[BUFSIZ];\r\n\r\n    printf(\"Your name: \");\r\n    fgets(buffer,BUFSIZ,stdin);\r\n    printf(\"Hello, %s\",buffer);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<pre>Your name: Danny\r\nHello, Danny<\/pre>\n<p>You don&#8217;t see it in the output, but originally I typed <strong>None of your business<\/strong>. Then I backspaced over this text to type <strong>Danny<\/strong> instead. Despite all this text being generated from standard input, the only characters received are <code>Danny<\/code>, which is shown in the output. This result is due to the terminal cooking the input.<\/p>\n<p>Cooked input, also referred to as <em>canonical mode<\/em>, is what configures line buffering, terminating input when the newline is encountered. It also interprets various control key combinations, such as ^H for backspace, ^U for erase line, ^W for erase word, ^I (tab) to advance to the next tab stop, and even the arrow keys to edit a line are interpreted to move the cursor and not read in their raw state.<\/p>\n<p>It&#8217;s possible to program the terminal to switch to raw or non-canonical mode. In this mode, the program run above may generate this output:<\/p>\n<pre>Your name: Bill^H^H^H^HDanny\r\nHello, Bill^H^H^H^HDanny<\/pre>\n<p>The user typed <strong>Bill<\/strong>, then erased this word and typed <strong>Danny<\/strong>. The backspace characters (^H) are retained as part of the input in raw or non-canonical mode.<\/p>\n<p>Obviously, users expect standard input to be cooked. You&#8217;ve most likely coded all your terminal window C programs this way. That&#8217;s great and to be expected, but it may happen that your program needs to read raw data. In such a case, it&#8217;s necessary for the program to reconfigure the terminal to accept input in raw mode. I cover this technique in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7350\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The terminal is sneaky when it comes to standard input. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7339\">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-7339","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\/7339","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=7339"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7339\/revisions"}],"predecessor-version":[{"id":7360,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7339\/revisions\/7360"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}