LearnFrenzy
Sign in Take a free test

Typedef · Predict the Output · Question 11

An array of typedef'd structs – count and average Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

typedef struct {
    const char *name;
    int marks;
} Student;

int main(void) {
    Student cls[] = {{"A", 70}, {"B", 80}, {"C", 90}};
    size_t n = sizeof cls / sizeof cls[0];
    int sum = 0;
    for (size_t i = 0; i < n; i++)
        sum += cls[i].marks;
    printf("%zu %d\n", n, sum / (int) n);
    return 0;
}
  • A 3 80 Correct
  • B 3 240
  • C 16 80
  • D 3 0
Correct Answer: Option A

The array size comes from its initializer (3 Students), and sizeof cls / sizeof cls[0] counts them. 240 / 3 = 80.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links