ProgrammingHard
char a[3] = {'x', 'y', 'z'}; Which call reads a correctly with no undefined behaviour?
- A. printf("%.3s", a);
- B. printf("%s", a);
- C. strlen(a);
- D. strcpy(b, a); where char b[3]
Correct answer: A. printf("%.3s", a);
a has no terminator, so any call that searches for '\0' (%s, strlen, strcpy) reads past the array. A precision of 3 stops exactly at the end of a.
More C Programming questions
- char a = "", b = "abc"; if (strlen(a) - strlen(b) > 0) puts("longer"); else puts("not longer"); What is printed?
- In C (not C++), what is sizeof('A')?
- char a[5] = "hi"; printf("%d", a[4]); What is printed?
- int n = -1; if (n < strlen("abc")) puts("yes"); else puts("no"); What is printed?
- int v = 0; for (const char p = "-42"; p >= '0' && p <= '9'; p++) v = v 10 + (p - '0'); printf("%d", v); What is printed?
- char d[4]; strncpy(d, "abcdef", 4); / d now has no terminator / Which call is safe to print d with?