ProgrammingHard
char *a = "xy"; char *b = a; b[0] = 'z'; printf("%s", a); What is printed?
- A. zy
- B. undefined behaviour, because the literal is not writable
- C. xy
- D. yz
Correct answer: B. undefined behaviour, because the literal is not writable
b points at the string literal, which cannot be modified. Writing to it is undefined.
More C Programming questions
- char buf[8] = "12"; int n = strlen(buf); printf("%d %d", n, (int)sizeof buf - n); What is printed?
- char z[6] = "ab0cd"; printf("%zu", strlen(z)); What is printed?
- int n = sprintf(NULL, "%s", "hello"); printf("%d", n); What is printed?
- int m = malloc(3 sizeof m); for (int i = 0; i < 3; i++) m[i] = malloc(4 sizeof m); / later / for (int i = 0; i < 3; i++) free(m[i])…
- int m[2][5]; printf("%zu", sizeof m[0]); What is printed on a platform with 4-byte int?
- int r = 2, c = 5; int m[r][c]; printf("%zu", sizeof m); What is printed on a platform with 4-byte int (C99 variable-length array)?