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.
#include <stdio.h> int putchar(int ch); int putc(int ch, FILE *stream);
Try it
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.
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
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 asputc(c, stdout). The standard allowsputc()to be a macro that evaluates its stream argument more than once, so never writeputc(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 callfflush(). - 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
charthat holds a negative value is converted properly, so you do not need a cast. In C, a character constant such as'a'has typeint, which is whyputchar('a')needs no conversion. - Speed. Writing one character per call is fine for small outputs. For large text,
fputs(),fwrite()orprintf()write whole chunks and are easier to read. - Checking errors. Most programs ignore the result of
putchar()onstdout, but when you write a file you should check forEOF(or checkferror()andfclose()at the end). - Reading counterparts are
getchar()andgetc().ungetc()pushes a character back onto an input stream. - Mixed with wide output. Do not combine
putchar()and wide functions such asputwchar()on the same stream.
Related
putsandfputs,printf,getcharandgetc,fflush- Lessons: Output, Characters, Text files