LearnFrenzy
Sign in Take a free test

C Const · Predict the Output · Question 12

C23 constexpr sizes for a 2D array – 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) {
    constexpr int ROWS = 2, COLS = 3;
    int grid[ROWS][COLS] = {};
    grid[ROWS - 1][COLS - 1] = 9;
    printf("%zu %d %d\n", sizeof grid / sizeof grid[0][0], grid[1][2], grid[0][0]);
    return 0;
}
  • A 6 9 0 Correct
  • B 6 9 garbage
  • C 5 9 0
  • D Compilation error
Correct Answer: Option A

ROWS and COLS are compile-time constants, so grid is an ordinary 2 × 3 array (not a VLA) and = {} zeroes it. 6 elements, the last set to 9.

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