LearnFrenzy
Sign in Take a free test

C Const · Find the Error · Question 1

Error: assigning to a const variable Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 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_attempts = 3;
    max_attempts = 5;
    printf("%d\n", max_attempts);
    return 0;
}
  • A const variables must be global
  • B max_attempts is const, so it cannot be assigned after its initialisation Correct
  • C printf cannot print a const
  • D There is no bug
Correct Answer: Option B

A const object gets its value only from its initializer. If the value really must change, the variable should not be const.

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