C programming stdio.h function - int putc(int char, FILE *stream)

ww‮ditfigi.w‬ea.com

The putc function in the stdio.h library of the C programming language is used to write a character to a specified stream. The syntax of the putc function is as follows:

int putc(int char, FILE *stream);

Here, the first argument char is the character to be written to the stream, and the second argument stream is a pointer to the FILE structure that represents the stream to write the character to.

The putc function writes the specified character to the 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:

#include <stdio.h>

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

In this example, the putc 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.