ProgrammingVery hard
char buf[] = "ab"; buf[2] = 'c'; printf("%zu", sizeof buf); What is printed?
- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
buf is initialised with three bytes from the literal. Writing buf[2] changes only the terminator, which keeps the size at three bytes.
More C Programming questions
- char s[3] = "ab"; printf("%zu", sizeof s); What is printed?
- char a = "xy"; char b = a; b[0] = 'z'; printf("%s", a); What is printed?
- 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])…