Latest web development tutorials

C library functions - putc ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint putc (int char, FILE * stream) The argument charspecified character (an unsigned character) is written to the specified stream in the stream, and the location identifier move forward.

statement

Here is () statement putc function.

int putc(int char, FILE *stream)

parameter

  • char - which it is to be written character.The character of its corresponding int value passed.
  • stream - This is a pointer to FILE pointer to an object, the object is FILE identifies the character to be written stream.

return value

This function as an unsigned char cast to an int Returns the character written, if an error occurs returns EOF.

Examples

The following example demonstrates putc () function is used.

#include <stdio.h>

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, which will create a filefile.txt in the current directory, it reads as follows:

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

Now let's use the following procedure to view the contents 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 standard library - <stdio.h>