ProgrammingModerate
char name[10]; /* input: a long name */ scanf("%9s", name); What is the most characters stored before the terminator?
- A. 10
- B. 8
- C. No limit: %s keeps reading until whitespace
- D. 9
Correct answer: D. 9
The field width caps the conversion at 9 characters, and scanf then stores the '\0' at index 9, exactly filling the 10-byte array. 10 forgets the terminator needs a byte, and 8 wrongly assumes the width already includes it.
More C Programming questions
- 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?
- For struct R { char c; short s; int i; } with natural alignment (char 1, short 2, int 4), what is sizeof(struct R)?
- Assume natural alignment with 4-byte int. What is sizeof(struct R[5]) where struct R is the eight-byte type from the previous record?