C programming stdio.h function - int putchar(int char)

www.‮ditfigi‬ea.com

The putchar function in the stdio.h library of the C programming language is used to write a single character to the standard output stream (stdout). The syntax of the putchar function is as follows:

int putchar(int char);

Here, the argument char is the character to be written to the standard output stream.

The putchar function writes the specified character to the standard output stream, and returns the character written on success. In case of an error, it returns EOF. The EOF macro is a constant defined in the stdio.h library that represents the end-of-file condition or an error condition.

For example, the following code writes the character 'a' to the standard output stream using the putchar function:

#include <stdio.h>

int main() {
   int result;
   result = putchar('a');
   if (result == EOF) {
      printf("Error writing to stream\n");
   }
   return 0;
}

In this example, the putchar function writes the character 'a' to the standard output stream (stdout). If the write operation is successful, the function returns the written character 'a', which is then stored in the result variable. If an error occurs during the write operation, the function returns EOF, and the code in the if statement is executed.