Return type of getchar(), fgetc() and getc() in C

CProgrammingServer Side Programming

Details about getchar(), fgetc() and getc() functions in C programming are given as follows −

The getchar() function

The getchar() function obtains a character from stdin. It returns the character that was read in the form of an integer or EOF if an error occurs.

A program that demonstrates this is as follows −

Example

 Live Demo

#include <stdio.h>

int main (){
   int i;

   printf("Enter a character: ");
   i = getchar();

   printf("\nThe character entered is: ");
   putchar(i);

   return(0);
}

Output

The output of the above program is as follows −

Enter a character: G
The character entered is: G

Now let us understand the above program.

The value obtained using the getchar() function is stored in i which is an integer variable. Then the character value is displayed using putchar(). The code snippet that shows this is as follows −

int i;

printf("Enter a character: ");
i = getchar();

printf("\nThe character entered is: ");
putchar(i);

The fgetc() function

The fgetc() function obtains a character from a file stream which is a pointer to a FILE object. This function returns the character that was read in the form of an integer or EOF if an error occurs.

A program that demonstrates this is as follows −

Example

 Live Demo

#include <stdio.h>

int main (){
   FILE *fp;
   fp = fopen("file.txt", "w");
   fprintf(fp, "Apple");
   fclose(fp);

   int i;

   fp = fopen("file.txt","r");
   
   if(fp == NULL){
      perror("Error in opening file");
      return(-1);
   }

   while((i=fgetc(fp))!=EOF){
      printf("%c",i);
   }

   fclose(fp);
   return(0);
}

Output

The output of the above program is as follows −

Apple

Now let us understand the above program.

First, the file is created and the data "Apple" is stored inside it. Then the file is closed. The code snippet that shows this is as follows −

FILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "Apple");
fclose(fp);

The file is opened again in reading mode. If fp is NULL then error message is displayed. Otherwise the contents of the file are displayed using the fgetc() function. The code snippet that shows this is as follows −

fp = fopen("file.txt","r");

if(fp == NULL){
   perror("Error in opening file");
   return(-1);
}

while((i=fgetc(fp))!=EOF){
   printf("%c",i);
}

fclose(fp);

The getc() function

The getc() function obtains a character from the stream that is specified. It returns the character that was read in the form of an integer or EOF if an error occurs.

A program that demonstrates this is as follows −

Example

 Live Demo

#include <stdio.h>

int main (){
   int i;

   printf("Enter a character: ");
   i = getc(stdin);

   printf("\nThe character entered is: ");
   putchar(i);

   return(0);
}

Output

The output of the above program is as follows −

Enter a character: K
The character entered is: K

Now let us understand the above program.

The getc() function obtains a character from the stream stdin as specified. This value is stored in int variable i. Then the character value is displayed using putchar(). The code snippet that shows this is as follows −

int i;

printf("Enter a character: ");
i = getc(stdin);

printf("\nThe character entered is: ");
putchar(i);
raja
Published on 06-Nov-2018 10:56:08
Advertisements