LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Find the Error · Question 5

Error: const variable assigned after its declaration Answer, explanation & discussion

  • Find the error
  • Easy
  • 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>

int main(void) {
    const int max;
    max = 10;
    printf("%d\n", max);
    return 0;
}
  • A const must be global
  • B A const object can only get its value from its initializer; assigning to it later is an error Correct
  • C max must be unsigned
  • D There is no bug
Correct Answer: Option B

Write const int max = 10;. A local const without an initializer is legal but useless – it holds garbage that can never be fixed.

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