ProgrammingHardProduct company OA
What is printed?
What is printed?
char *label(int n) {
static char buf[8];
buf[0] = 'A' + n;
buf[1] = '\0';
return buf;
}
int main(void) {
char *a = label(1);
char *b = label(2);
printf("%s %s %d", a, b, a == b);
}- A. B C 1
- B. B C 0
- C. C C 1
- D. C C 0
Correct answer: C. C C 1
Both calls return the address of the same static buffer, so a == b and the second call overwrites the 'B' with 'C' before anything is printed. Returning a static buffer is legal (unlike returning a local array) but every caller shares it.
More C Programming questions
- What is printed? c int i, s = 0; for (i = 1; i < 50; i = i 2 + 1) s += i % 3 ? i : -i; printf("%d %d", s, i);
- Assume ASCII. What is printed? c char s[] = "aB3"; for (char p = s; p; p++) p ^= 32; printf("%d %d %d", s[0], s[1], s[2]);
- What is printed? c void f(int p, int n) { if (n == 0) return; p += n; f(p, n - 1); p = 2; } int main(void) { int x = 0; f(&x, 3)…
- On an LP64 system (8-byte pointers), what is printed? c printf("%zu %zu", sizeof("abc"), sizeof("abc" + 1));
- On an LP64 system (8-byte pointers), what is printed? c char a[] = "cat", b = "cat"; printf("%d %d", strcmp(a, b) == 0, sizeof(a) ==…
- What is printed? c struct P { int x; int y; }; struct P a[3] = {{1, 2}, {3, 4}, {5, 6}}; struct P p = a; int r1 = (p + 2)->x; int r2 =…