C File I/O Operations : putc()

The putc() function is used to writes a single character into a file. It gives a single character output at a time to the output stream. The basic syntax of putc() is :
 int putc( int c, FILE *fp);
Where c specifies the character value and file pointer fp specifies the file pointer, in which character needs to be written. On success it return the character itself and on the error condition it returns the EOF.

Now lets take a look at example C program :
#include <stdio.h>

int main() {
  FILE *fp;
  char ch;

  fp = fopen("myfile.txt", "w");
  printf("Enter Some Text(Press Enter to exit) :\n");
  while((ch=getchar()) != '\n') {
    putc(ch, fp);

  return 0;
Output :

Enter Some Text(Press Enter to exit) :
Hello world This is Test String

The above C program create a file myfile.txt and writes the user given inputs on it.

Topics :