ProgrammingVery hardProduct-company MCQ pattern

What is printed? (This program has no undefined behavior.)

What is printed? (This program has no undefined behavior.)
char *fmt(int n) {
    static char buf[16];
    sprintf(buf, "%d", n);
    return buf;
}
int main(void) {
    char *arr[3];
    for (int i = 0; i < 3; i++) arr[i] = fmt(i * 10);
    printf("%s %s %s", arr[0], arr[1], arr[2]);
}
  1. A. Undefined behavior: garbage values
  2. B. 0 10 20
  3. C. 0 0 0
  4. D. 20 20 20

Correct answer: D. 20 20 20

All three pointers in arr alias the SAME static buf, which gets overwritten on every call. By the time printf runs, buf holds "20" (the last call's result), so all three %s conversions read that same final content — well-defined, just logically wrong.

More C Programming questions

Exams that ask this topic