Question 4
Back to Exercise
At file scope, is const int N = 10; int buf[N]; valid C?
- A Yes, exactly as in C++
- B No – in C a const variable is not a constant expression, and VLAs are not allowed at file scope Correct
- C Yes, but only if N is static
- D Only with -O2
Correct Answer: Option B
C++ treats a const int initialised with a constant as a constant expression; C does not. Use enum { N = 10 };, #define N 10 or C23 constexpr int N = 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!