The putc() function is defined in the stdio.h header file. It helps to write the character specified in the argument to the given stream. And it also advances the position indicator for the stream.
int putc(int char, FILE *stream); #where stream should be a file pointer
The putc() function takes two parameters, the stream on which the write operation is performed and the character to be written.
Parameter | Description | Required / Optional |
---|---|---|
char | This is the character to be written | Required |
stream | Pointer to a FILE object that identifies the stream where the character is to be written | Required |
In putc() function character values are returned as an unsigned char cast to an int or EOF on end of file or error.
Input | Return Value |
---|---|
on success | character written |
on error | set the stream's error indicator and return EOF |
#include <stdio.h>
int main()
{
FILE *pnt;
int chr;
pnt = fopen("myfile.txt", "w");
for( chr = 33 ; chr <= 100; chr++ )
{
putc(chr, pnt);
}
fclose(pnt);
return(0);
}
Output:
/* myfile.txt content */ !"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`abcd
#include <stdio.h>
int main (){
char chr;
FILE *pnt;
if (pnt = fopen("mytest.c", "r"))
{
chr = getc(pnt);
while (chr != EOF)
{
putc(chr, stdout);
chr = getc(pnt);
}
fclose(pnt);
return 0;
}
return 1;
}
Output:
/* mytest.c file content */ Hi, How are you?