Easy Tutorial
❮ C Function Fsetpos C Function Memset ❯

C Library Function - fputc()

C Standard Library - <stdio.h>

Description

The C library function int fputc(int char, FILE *stream) writes the character specified by the argument char (an unsigned character) to the specified stream and advances the position indicator.

Declaration

Here is the declaration for the fputc() function.

int fputc(int char, FILE *stream)

Parameters

Return Value

On success, the character written is returned. If an error occurs, EOF is returned and the error indicator is set.

Example

The following example demonstrates the use of the fputc() function.

#include <stdio.h>

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

   fp = fopen("file.txt", "w+");
   for( ch = 33 ; ch <= 100; ch++ )
   {
      fputc(ch, fp);
   }
   fclose(fp);

   return(0);
}

Let's compile and run the above program, which will create a file file.txt in the current directory with the following content:

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd

Now, let's use the following program to view the content of the above file:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

C Standard Library - <stdio.h>

❮ C Function Fsetpos C Function Memset ❯