LearnFrenzy
Sign in Take a free test

C Const · Find the Error · Question 5

Error: const int used as a global array size Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

const int SIZE = 10;
int buffer[SIZE];

int main(void) {
    buffer[0] = 1;
    printf("%d\n", buffer[0]);
    return 0;
}
  • A buffer must be static
  • B In C, a const int is not a constant expression, so it cannot size a file-scope array (that would be a VLA, which is not allowed there) Correct
  • C SIZE must be unsigned
  • D There is no bug
Correct Answer: Option B

This compiles in C++, but not in C. Use enum { SIZE = 10 };, #define SIZE 10, or C23 constexpr int SIZE = 10;.

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