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);
}
  1. A. B C 1
  2. B. B C 0
  3. C. C C 1
  4. 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

Exams that ask this topic