ProgrammingModerate
char buf[] = "abc\n"; buf[strcspn(buf, "\n")] = '\0'; printf("%zu", strlen(buf)); What is printed?
- A. 3
- B. 4
- C. 2
- D. 0
Correct answer: A. 3
strcspn returns the index of the newline, 3, and the terminator replaces it. The length is therefore 3 and the newline is gone.
More C Programming questions
- Why must the result of getchar() be stored in an int rather than a char?
- 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?