Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C putchar() and putc() functions

Reference for C putchar(), putc() and fputc(): write one character to stdout or a file stream, what they return, and why the argument is converted to unsigned char.

putchar() writes a single character to standard output. putc() does the same for any stream you pass it, and fputc() is its function-only twin. They are the lowest-level output calls in <stdio.h>: everything else, from puts() to printf(), can be built from them.

The character is passed as an int but written as an unsigned char, so putchar(200) and putchar(-56) write the same byte. The return value is that byte as a non-negative int, or EOF if writing failed.

C
#include <stdio.h>

int putchar(int ch);
int putc(int ch, FILE *stream);

Try it

C Printing characters one by one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C putc to stdout, stderr and a file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int putchar(int ch);
int putc(int ch, FILE *stream);
int fputc(int ch, FILE *stream);
Name Description
ch The character to write. It is converted to unsigned char before it is written.
stream An open stream that allows writing: stdout, stderr, or the result of fopen() with a write or append mode.

Returns: the character written, converted to unsigned char and then to int, or EOF on a write error (the stream's error indicator is set; test it with ferror()).

Errors / undefined behavior: passing a stream that is NULL, already closed, or opened for reading only is undefined behavior.

Usage notes

  • putchar(c) is the same as putc(c, stdout). The standard allows putc() to be a macro that evaluates its stream argument more than once, so never write putc(c, files[i++]). fputc() is always a real function and evaluates its arguments once.
  • No newline is added. Output to a terminal is usually line buffered, so characters may not appear until you print '\n' or call fflush().
  • Characters are bytes. putchar() knows nothing about UTF-8. Writing a multi-byte character one byte at a time works only if you write every byte of it; putchar('รฉ') with a multi-character constant does not do what you want.
  • Type of the argument. A char that holds a negative value is converted properly, so you do not need a cast. In C, a character constant such as 'a' has type int, which is why putchar('a') needs no conversion.
  • Speed. Writing one character per call is fine for small outputs. For large text, fputs(), fwrite() or printf() write whole chunks and are easier to read.
  • Checking errors. Most programs ignore the result of putchar() on stdout, but when you write a file you should check for EOF (or check ferror() and fclose() at the end).
  • Reading counterparts are getchar() and getc(). ungetc() pushes a character back onto an input stream.
  • Mixed with wide output. Do not combine putchar() and wide functions such as putwchar() on the same stream.