English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

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

For detailed informationgetchar(),fgetc()Andgetc()The C programming function is given as follows-

Thisgetchar()Method

Thisgetchar()The function retrieves a character from stdin. If an error occurs, it returns the read character as an integer or EOF.

The program to demonstrate this is as follows-

Example

#include <stdio.h>
int main() {
   int i;
   printf("Enter a character: ");
   i = getchar();
   printf("\nThe character entered is: ");
   putchar(i);
   return(0);
}

Output Result

The output of the above program is as follows-

Enter a character: G
The character entered is: G

Now let's understand the above program.

Use thegetchar()The value obtained by the function is stored in i, which is an integer variable. Then it is used to display the character value.putchar()The following code segment shows this.-

int i;
printf("Enter a character: ");
i = getchar();
printf("\nThe character entered is: ");
putchar(i);

Thisfgetc()Method

Thisfgetc()The function retrieves a character from the file stream, which is a pointer to the FILE object. If an error occurs, this function returns the read character as an integer or EOF.

The program to demonstrate this is as follows-

Example

#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 Result

The output of the above program is as follows-

Apple

Now let's understand the above program.

First, create the file and store the data "Apple" in it. Then close the file. The following code segment shows this.-

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

Open the file again in read mode. If fp is NULL, display an error message. Otherwise, use thefgetc()The method to display the content of the file is shown in the following code segment.-

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);

Thisgetc()Method

Thisgetc()The function retrieves a character from the specified stream. If an error occurs, it returns the read character as an integer or EOF.

The program to demonstrate this is as follows-

Example

#include <stdio.h>
int main() {
   int i;
   printf("Enter a character: ");
   i = getc(stdin);
   printf("\nThe character entered is: ");
   putchar(i);
   return(0);
}

Output Result

The output of the above program is as follows-

Enter a character: K
The character entered is: K

Now let's understand the above program.

Thisgetc()The function retrieves the specified character from the stream stdin. This value is stored in the int variable i. Then it is used to display the character value.putchar()The following code segment shows this.-

int i;
printf("Enter a character: ");
i = getc(stdin);
printf("\nThe character entered is: ");
putchar(i);