ProgrammingModerate
What is the smallest array size that holds the string "Ravi" with strcpy and no undefined behaviour?
- A. 4
- B. 6
- C. 5
- D. 8
Correct answer: C. 5
Four letters plus the terminating '\0' need five bytes. A size of four makes strcpy write one byte past the end.
More C Programming questions
- char s[] = "x41x420x43"; printf("%zu", strlen(s)); What is printed?
- char s[] = "banana"; char p = strrchr(s, 'a'); printf("%ld", (long)(p - s)); What is printed?
- printf("%zu", strcspn("aXbYc", "XY") + strspn(" 42ab", " 0123456789")); What is printed?
- A naive strstr-style search looks for a pattern of length m = 50000 in a text of length n = 100000. The text is all 'a' and the pattern is…
- What does strncmp("abc", "xyz", 0) return?
- char a[3] = {'x', 'y', 'z'}; Which call reads a correctly with no undefined behaviour?