Input and Diagnostics in a Local Terminal
Read a line in a local terminal
The earlier lessons used values already in the program. Reading from the keyboard adds a new source of failure: input may end, be too long, or have the wrong form.
| Stream | Purpose |
|---|---|
stdin | Input, often the keyboard in a local terminal |
stdout | Ordinary results and prompts |
stderr | Diagnostics, such as a failed input request |
A terminal may display stdout and stderr together, but they remain separate streams. None of these names promises access to persistent operating-system files.
Optional local practice
The course's browser Run control does not provide an interactive keyboard prompt. Do not paste an input-waiting program into a graded exercise. The examples below are for a C compiler and terminal on your own computer, not browser Run blocks.
Save this program as main.c. In that directory, compile with cc -std=c11 -Wall -Wextra -Wpedantic -Werror main.c -o main, then run ./main. Type a short name and press Enter.
#include <stdio.h>
#include <string.h>
int main(void) {
char name[32];
fputs("Name: ", stdout);
fflush(stdout);
if (fgets(name, sizeof name, stdin) == NULL) {
fputs("No name read\n", stderr);
return 1;
}
if (strchr(name, '\n') == NULL) {
fputs("Use a shorter name and press Enter\n", stderr);
return 1;
}
printf("Registered: %s", name);
return 0;
}
fflush(stdout) asks the stream to deliver the prompt before the input call; the prompt has no newline. On success, fgets reads at most 31 characters into this 32-byte array, stops after a newline if it reaches one, and adds the null terminator. A NULL return means no usable input string was obtained, so that branch must not print name.
strchr, declared in <string.h>, searches a string for a character and returns its address, or NULL if it is absent. Here it checks whether the line's newline reached the buffer. We reject input without that newline and end the program rather than accidentally accept the first part of a long name. This also rejects a final unterminated line at end of input. A program that asked again would first need to deal with unread characters left in stdin.
For input Mira followed by Enter, the terminal shows the prompt and then Registered: Mira. fgets kept the newline, so the final printf does not add another. Never use gets: it cannot enforce the buffer's capacity and is not part of C11.
Reading one character
getchar() returns an int, not a char. The result must represent a character value or the special result EOF, which signals end of input or a read error. Test that result before treating it as a character:
#include <stdio.h>
int main(void) {
int ch = getchar();
if (ch == EOF) {
fputs("No character read\n", stderr);
return 1;
}
putchar(ch);
putchar('\n');
return 0;
}
Compile this as a separate program. Enter A and press Enter; it prints A. It consumes only the first character, not the rest of the line. Do not store getchar() directly in a char and then test for EOF: the conversion can lose the distinction.
You may also encounter scanf. It returns the number of input items successfully assigned, so one requested conversion needs a return value of 1 before you use its destination. A %d input conversion needs the address of an int, unlike %d output, which needs the value. A failed conversion can leave input unread; numeric input outside the destination type's range is another reason not to treat a return-value check as a complete validation strategy. For this course, use the bounded line example above and keep parsing user-entered numbers as a later topic.
The browser exercises remain independent of stdin and stderr capture. Finish the section quiz using the lifetime, formatting, and stream distinctions you have just practiced.