The getc() function is defined in the stdio.h header file. It helps to get the next character from the stream specified in the arguments. And it also advances the position indicator for the stream.
int getc(FILE *stream); #where stream should be a file pointer
The getc() function takes a single parameter stream on which the operation is to be performed.
Parameter | Description | Required / Optional |
---|---|---|
stream | the pointer to a FILE object that identifies the stream. | Required |
In getc() 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 | returns next requested object |
on error or EOF | int value |
#include <stdio.h>
int main()
{
char chr;
printf("Enter the character: ");
chr = getc(stdin);
printf("Your entered character is: ");
putc(chr, stdout);
return(0);
}
Output:
Enter the character: P Your entered character is: P
#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?