LearnFrenzy
Sign in Take a free test

Typedef · Predict the Output · Question 12

C23 typeof with an array and an element – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following program, compiled as C23?

main.c
#include <stdio.h>

int main(void) {
    double rates[] = {1.5, 2.5};
    typeof(rates[0]) sum = 0;
    typeof(rates) copy = {rates[1], rates[0]};
    sum = copy[0] + copy[1];
    printf("%zu %zu %.1f\n", sizeof sum, sizeof copy, sum);
    return 0;
}
  • A 8 16 4.0 Correct
  • B 8 8 4.0
  • C 16 16 4.0
  • D Compilation error
Correct Answer: Option A

typeof(rates[0]) is double (8 bytes); typeof(rates) is the whole array type double[2] (16 bytes) – no decay to a pointer. The sum is 2.5 + 1.5 = 4.0.

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