Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - putc()

C Library Function - putc()

C Standard Library - <stdio.h> C Standard Library -

Description

C library function *int putc(int char, FILE stream) Converts the string pointed to by the argument char Write the specified character (an unsigned char) to the specified stream stream and advance the position indicator.

Declaration

The following is the declaration of the putc() function.



int putc(int char, FILE *stream)

Parameters

Return Value

The function returns the character written, cast from unsigned char to int, or EOF if an error occurs.

Example

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



#include 



int main ()

{

   FILE *fp;

   int ch;



   fp = fopen("file.txt", "w");

   for( ch = 33 ; ch <= 100; ch++ ) 

   {

      putc(ch, fp);

   }

   fclose(fp);

   

   return(0);

}

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



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

Now let us use the following program to view the content of the file above:



#include 



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 Standard Library -