fgetc Function in C Programming

fgetc Function

Declaration: int fgetc(FILE *stream);

The fgetc() function returns the next character from the specified input stream and increments the file position indicator. The character is read as an unsigned char that is converted to an integer. If the end-of-file is reached, fgetc() returns EOF. If fgetc() encounters an error, EOF is also returned.

For example, the following program reads and displays the content of text file.

#include<stdio.h>
#include<stdlib.h>
int main()
             {
             FILE *fptr;
             If((fptr = fopen("TEST","r"))==NULL)
             {
             printf("Cannot open file\n");
             exit(1);
             }
             while((c=fgetc(fptr))!=EOF)
             puchar(c);
             if(fclose(fptr))
             pritf("File close error\n");
             return 0;
            
             } 

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!