C library functions -putc()
C Standard Library - <stdio.h>
Description
C Library Functionsint putc(int char, FILE *stream)Pass the parametercharThe specified character (an unsigned char) is written to the specified stream, and the position indicator is advanced forward.
Declaration
The following is the declaration of the putc() function.
int putc(int char, FILE *stream)
Parameter
- char-- This is the character to be written. The character is passed as its corresponding int value.
- stream-- This is a pointer to a FILE object that identifies the stream to which the character is to be written.
Return Value
This function returns the written character as an unsigned char cast to an int, or returns EOF if an error occurs.
Example
The following example demonstrates the usage of the putc() function.
#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 us compile and run the above program, which will create a file in the current directoryfile.txt, and its contents are as follows:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
Now let's use the following program 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);
}
other extensions