ProgrammingModerate
Why must the result of getchar() be stored in an int rather than a char?
- A. Because int is faster than char on all hardware
- B. Because EOF (-1) must be distinguishable from every byte value, and a char may be unsigned and unable to hold -1
- C. Because getchar returns a pointer to a char
- D. Because char cannot hold a newline character
Correct answer: B. Because EOF (-1) must be distinguishable from every byte value, and a char may be unsigned and unable to hold -1
getchar returns every unsigned char value as a non-negative int plus the negative EOF. A char that is unsigned cannot hold -1, so the loop test would never see end of file.
More C Programming questions
- Which call reads a line into char buf[100] safely?
- char name[10]; / input: a long name / scanf("%9s", name); What is the most characters stored before the terminator?
- Records (name, score, age) are sorted by score descending, and by age ascending for equal scores. Input: (A,90,30), (B,90,25), (C,85,20)…
- qsort is used on an array of records where several records have equal keys. Which statement is guaranteed by the C standard?
- A sorted array of struct items is searched with bsearch for a key that is not in the array. What does bsearch return?
- An array struct P ps[] of pointers to records is to be sorted by an int field k with qsort. Which comparator is correct?