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.
char ch; /* May cause problems */ while ((ch = getchar()) != EOF) { putchar(ch); } |
chevron_right
filter_none
Here is a version that uses integer to compare the value of getchar().
int in; while ((in = getchar()) != EOF) { putchar(in); } |
chevron_right
filter_none
See this for more details.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between getc(), getchar(), getch() and getche()
- EOF, getc() and feof() in C
- fgetc() and fputc() in C
- Implicit return type int in C
- Difference between Type Casting and Type Conversion
- Return values of printf() and scanf() in C/C++
- C function argument and return values
- What does main() return in C and C++?
- How can I return multiple values from a function?
- return statement vs exit() in main()
- Return from void functions in C++
- How to return multiple values from a function in C or C++?
- return statement in C/C++ with Examples
- Data type of character constants in C and C++
- Data Type Ranges and their macros in C++
- Conversion of Struct data type to Hex String and vice versa
- C | Storage Classes and Type Qualifiers | Question 1
- C | Storage Classes and Type Qualifiers | Question 3
- C | Storage Classes and Type Qualifiers | Question 19
- C | Storage Classes and Type Qualifiers | Question 19

