C++ putchar()

The putchar() function in C++ writes a character to stdout.

putchar() prototype

int putchar(int ch);

The putchar() function takes an integer argument to write it to stdout. The integer is converted to unsigned char and written to the file. A call to putchar(ch) is equivalent to putc(ch, stdout).

It is defined in <cstdio> header file.

putchar() Parameters

ch: The character to be written.

putchar() Return value

On success, the putchar() function returns the character represented by ch. On failure, it returns EOF and sets the error indicator on stdout.

Example: How putchar() function works

#include <cstdio<

int main()
{   
    for (int i=48; i<58; i++)
    {
        /*  Writes the equivalent character */
        putchar(i);
        putchar(' ');
    }
    
    return 0;
}

When you run the program, the output will be:

0 1 2 3 4 5 6 7 8 9