LearnFrenzy
Sign in Take a free test

C Const · Predict the Output · Question 11

Changing a value through a non-const alias – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int value = 1;
    const int *view = &value;
    int *edit = &value;
    *edit = 99;
    printf("%d\n", *view);
    return 0;
}
  • A 99 Correct
  • B 1
  • C Compilation error
  • D Undefined behaviour
Correct Answer: Option A

view and edit point to the same int. The const on view limits only what can be done through view; the object is not const, so writing through edit is perfectly legal.

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