fungetc

The getc() function in the C programming language is used to read a character from a file. It is a file handling function that allows you to read characters one by one from a file. The getc() function takes a file pointer as an argument and returns the next character from the file.

Here is an example of how to use the getc() function:

#include <stdio.h>

int main() {
   FILE *file;
   int character;

   file = fopen("example.txt", "r"); // Open the file in read mode

   if (file == NULL) {
      printf("Unable to open the file.\n");
      return 1;
   }

   while ((character = getc(file)) != EOF) { // Read characters until the end of file
      printf("%c", character); // Print the character
   }

   fclose(file); // Close the file

   return 0;
}

In this example, we first declare a file pointer file and an integer variable character. We then use the fopen() function to open the file "example.txt" in read mode. If the file cannot be opened, we display an error message and return from the program.

Inside the while loop, we use the getc() function to read characters from the file one by one until the end of the file is reached (EOF is the end-of-file constant). We store each character in the character variable and then print it using printf().

Finally, we close the file using the fclose() function to free up system resources.

Please note that the getc() function reads characters as integers, so you may need to cast the result to a char if you want to treat it as a character.

I hope this explanation helps! Let me know if you have any further questions.