What is the getc function in C?

The getc function in C reads the next character from any input stream and returns an integer value. It is a standard function in C and can be used by including the <stdio.h> header file.

Prototype

int getc(FILE *stream)

Parameter and return value

Parameter:

  • stream: A file stream or standard input stream identified by a pointer to the FILE object.

Return value:

  • int: Integer casting of the character read by getc. If the read operation is unsuccessful due to an error or the end of a file is reached, EOF is returned.

Code

#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
c = getc(stdin);
printf("\nCharacter is: %c", c); // prints the character on standard output, /n is used to add a newline
return 0;
}

Enter the input below

In the code example above, the user is prompted for a character from standard input, and the value entered is read by the getc function. The value read is then stored in the c variable and displayed.

main.c
sample.txt
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt","r"); //file pointer opens the file in read mode
printf("Character read from file is: %c", getc(fp)); //file content is: educative
return 0;
}

In the example above, the sample.txt file is opened in read mode. The position pointed to by the file pointer (fp) is retrieved through getc and displayed.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved