Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

putchar — write a single character

putchar writes one character to stdout. It returns the written character, or EOF on a write error.

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

Syntax

#include <stdio.h>
int putchar(int c);

putchar(c), declared in <stdio.h>, corresponds to putc(c, stdout). The value of c is converted to unsigned char before writing. The function neither prints the argument's numeric representation nor automatically adds a newline; use '\n'.

The result has type int: the written character, or EOF on an error, which also sets the stream's error indicator. Check it before converting to char. Buffering can delay writing; the example also checks fflush(stdout). This is byte output, not output of an arbitrary Unicode character.

Examples

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

int main(void)
{
    if (putchar('A') == EOF || putchar('\n') == EOF) {
        return EXIT_FAILURE;
    }
    if (fflush(stdout) == EOF) {
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Result

A

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