Node:putchar, Next:, Previous:getchar, Up:Single-character input and output



putchar

If you want to print a single character on standard output, you can use the putchar function. It takes a single integer parameter containing a character (the argument can be a single-quoted text character, as in the example below), and sends the character to stdout. If a write error occurs, putchar returns EOF; otherwise, it returns the integer it was passed. This can simply be disregarded, as in the example below.

Here is a short code example that makes use of putchar. It prints an X, a space, and then a line of ten exclamation marks (!!!!!!!!!!) on the screen, then outputs a newline so that the next shell prompt will not occur on the same line. Notice the use of the for loop; by this means, putchar can be used not just for one character, but multiple times.

#include <stdio.h>

int main()
{
  int i;

  putchar ('X');
  putchar (' ');
  for (i=1; i<=10; i++)
    {
      putchar ('!');
    }
  putchar ('\n');

  return 0;
}