getc macro : Read Character from Stream (stdio.h Header file)
- getc is a macro.
- getc accepts one character from a stream
- Header file : Stdio.h
- getc returns the next character on the given input stream .
- Getc increments the stream’s file pointer to point to the next character.
- getc returns the character read, after converting it to an int without sign extension.
Syntax :
int getc(FILE *stream);
Live Example :
int main(void)
{
char ch;
printf("\nInput a character:");
/* read a character from the standard
input stream */
ch = getc(stdin);
printf("\nThe character input was : '%c'", ch);
return 0;
}
Output :
Input a character : P
The character input was : 'P'
Another Live Example :
#include<stdio.h>
int main()
{
int ch;
FILE *fp;
if (fp = fopen("file.txt", "rt"))
{
ch = getc(fp);
while (ch != EOF)
{
printf("%c", ch);
ch = getc(fp);
}
fclose(fp);
}
return 0;
}
Output :
It will read complete file till we get EOF
Related Articles:
- Fgetc function : Get character from stream (stdio.h)
- fgetchar >> Gets Character from stdin >> Stdio.h Header File
- fputchar >> Stdio.h >> Header File in C Programming
- Fgets : Read Line of Character from File
- Fputc : Write Single Character to specified Stream
- File Handling / File Operations in C Programming
- Putc : Write Single Character to specified Stream
- Stdio.h >> clearerr function
- EOF and feof function >> stdio.h >> File Handling in C
- File open mode : File Stream in C Programming
About the author: Pritesh View all posts by Pritesh
My name is Pritesh Taral. I am working in well known MNC. as Java Developer. I am part time blogger loves writing articles on C/C++.
I am active on facebook using community fan page .One can Visit me @ Facebook
Facebook Fan Page