getc() function in C language with Example

Here, we are going to learn about the getc() function of library header stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on January 24, 2019

getc() function in C


    int getc(FILE *filename);


    FILE *filename

Return type: int

Use of function:

In the file handling, through the getc() function we take the next character from the input file stream and increment the file position pointer. The prototype of the function getc() is:

    int getc(FILE *filename);

It returns an integer value which is conversion of an unsigned char. It also returns EOF which itself is also an integer value. Whenever there is a binary file, check for EOF with the function feof().

getc() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
    //Initialize the file pointer
    FILE* f;
    char ch;
    //Create the file for write operation
    f = fopen("includehelp.txt", "w");
    printf("Enter five character\n");
    for (int i = 0; i < 5; i++) {
        //take the characters from the users
        scanf("%c", &ch);
        //write back to the file
        putc(ch, f);
        //clear the stdin stream buffer
    //close the file after write operation is over
    //open a file
    f = fopen("includehelp.txt", "r");
    printf("Write operation is over and file is ready for read operation\n");
    printf("\n...............print the characters..............\n\n");
    while (!feof(f)) {
        //takes the characters in the character array
        ch = getc(f);
        //and print the characters
        printf("%c\n", ch);

    return 0;


getc example in c

C stdio.h Library Functions Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.