LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Predict the Output · Question 2

Designated array initializer and a short string Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

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

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    int a[5] = {[1] = 4, 7};
    char s[6] = "ab";
    printf("%d %d %d %d %zu %d\n", a[0], a[1], a[2], a[4], strlen(s), s[4]);
    return 0;
}
  • A 0 4 7 0 2 0 Correct
  • B 4 7 0 0 2 0
  • C 0 4 0 7 2 0
  • D 0 4 7 0 6 0
Correct Answer: Option A

[1] = 4 sets a[1]; the next value, 7, goes to a[2]; the rest are 0. The string fills s[0]–s[2] (including '\0') and the remaining bytes are zeroed.

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