LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 5

Output of a partly initialised 2D array in C Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int m[2][3] = {{1}, {4, 5}};
    for (int r = 0; r < 2; r++)
        for (int c = 0; c < 3; c++)
            printf("%d ", m[r][c]);
    printf("\n");
    return 0;
}
  • A 1 4 5 0 0 0
  • B 1 0 0 4 5 0 Correct
  • C 1 1 1 4 5 5
  • D Garbage values
Correct Answer: Option B

Each inner brace fills one row, and missing elements of a row become 0: row 0 = 1 0 0, row 1 = 4 5 0. Without the inner braces the values would fill row 0 first.

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.

Popular