ProgrammingHardProduct company OA

What is printed?

What is printed?
struct E { char name[8]; int age; };
struct E t[] = {{"Asha", 31}, {"Bilal", 27}, {"Chen", 45}, {"Dev", 22}};
struct E *p = t, *best = t;
for (; p < t + 4; p++)
    if (p->age > best->age) best = p;
printf("%s %td %c", best->name, best - t, (best - 1)->name[1]);
  1. A. Chen 2 h
  2. B. Chen 3 i
  3. C. Chen 2 i
  4. D. Asha 0 s

Correct answer: C. Chen 2 i

The loop keeps a pointer to the oldest entry, Chen (45) at index 2, and best − t counts structs, not bytes. best − 1 is Bilal's entry, whose name[1] is 'i'. 'h' takes name[1] of Chen instead.

More C Programming questions

Exams that ask this topic