What is return type of getchar(), fgetc() and getc() ?

In C, return type of getchar(), fgetc() and getc() is int (not char). So it is recommended to assign the returned values of these functions to an integer type variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

char ch;  /* May cause problems */  
while ((ch = getchar()) != EOF) 
{
   putchar(ch);
}

chevron_right


Here is a version that uses integer to compare the value of getchar().

filter_none

edit
close

play_arrow

link
brightness_4
code

int in;  
while ((in = getchar()) != EOF) 
{
   putchar(in);
}

chevron_right


See this for more details.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


10


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.