Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

stdin — standard input stream

`stdin` from `<stdio.h>` points to the standard input stream. Pass it to input functions; it does not need to be opened first.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdio.h>
stdin

stdin is a macro from <stdio.h> expanding to an expression of type FILE *. In a hosted environment, it points to the standard text input stream, opened at program startup. It does not exclusively mean the keyboard.

The example reads one line fragment. fgets retains a newline it reads and appends a null character; the buffer holds at most 31 input characters. The remainder of a longer line stays in the stream.

fgets returns NULL on a read error or if it reaches end-of-file without reading any characters. ferror(stdin) tests whether the stream’s error indicator is set, not whether the most recent fgets call failed because of a read error. If an earlier error left the indicator set, ferror(stdin) can still return a nonzero value after a later fgets returns NULL at end-of-file. The check distinguishes the two cases in this example because the fgets call is the first operation on stdin. Do not use fflush(stdin) to discard input: standard C does not define that use.

Examples

#include <stdio.h>
#include <stdlib.h>

/* To reproduce the sample output, supply Hello followed by a newline on stdin. */
int main(void)
{
    char line[32];

    if (fgets(line, sizeof line, stdin) == NULL) {
        return ferror(stdin) ? EXIT_FAILURE : EXIT_SUCCESS;
    }
    if (fputs(line, stdout) == EOF || fflush(stdout) == EOF) {
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Result

Hello

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources