Cooked or Raw?

When the waitress asks how I like my eggs, I answer, “Cooked.”

O, how we all laugh . . .

The humor here is that it’s assumed you desire your eggs to be cooked and the waitress’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.

Just like ordering eggs in a diner, standard input for a computer terminal is assumed to be in the “cooked” mode. This setting doesn’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’t even think about it.

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’t feed into standard input; the ^H (control-H) character isn’t in the queue. In fact, even the Enter key press is assumed to signal the end of input.

The following code reads standard input and outputs the text input.

2026_01_10-Lesson.c

#include <stdio.h>

int main()
{
    char  buffer[BUFSIZ];

    printf("Your name: ");
    fgets(buffer,BUFSIZ,stdin);
    printf("Hello, %s",buffer);

    return 0;
}

Here’s a sample run:

Your name: Danny
Hello, Danny

You don’t see it in the output, but originally I typed None of your business. Then I backspaced over this text to type Danny instead. Despite all this text being generated from standard input, the only characters received are Danny, which is shown in the output. This result is due to the terminal cooking the input.

Cooked input, also referred to as canonical mode, 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.

It’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:

Your name: Bill^H^H^H^HDanny
Hello, Bill^H^H^H^HDanny

The user typed Bill, then erased this word and typed Danny. The backspace characters (^H) are retained as part of the input in raw or non-canonical mode.

Obviously, users expect standard input to be cooked. You’ve most likely coded all your terminal window C programs this way. That’s great and to be expected, but it may happen that your program needs to read raw data. In such a case, it’s necessary for the program to reconfigure the terminal to accept input in raw mode. I cover this technique in next week’s Lesson.

Leave a Reply