ProgrammingHard
char s[] = "xyz"; char *p = s; p[0] = 'a'; printf("%s %zu", s, sizeof s); What is printed?
- A. xyz 4
- B. ayz 3
- C. axyz 4
- D. ayz 4
Correct answer: D. ayz 4
p points into the writable array s, not into a read-only literal, so the store is legal. sizeof s counts the terminator as well, giving 4.
More C Programming questions
- char b[4]; int r = snprintf(b, 4, "%d", 123456); printf("%d %s", r, b); What is printed?
- int pal(const char s) { int i = 0, j = (int)strlen(s) - 1; while (i < j) if (s[i++] != s[j--]) return 0; return 1; } How many of "a"…
- char s[] = "xyz"; for (int i = 0; s[i]; i++) s[i] = (s[i] - 'a' + 3) % 26 + 'a'; printf("%s", s); What is printed?
- char d[8] = "#######"; strncpy(d, "ab", 5); int z = 0; for (int i = 0; i < 8; i++) if (d[i] == 0) z++; printf("%d", z); What is printed?
- int a = 0, b = 0; char op = 0; int n = sscanf("12 + x", "%d %c %d", &a, &op, &b); printf("%d %c", n, op); What is printed?
- Which loop is safe for every value a plain char can hold, including bytes above 127 that may be negative?