LearnFrenzy
Sign in Take a free test

C Const · Predict the Output · Question 1

The object behind a pointer to const can still change Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 5, y = 7;
    const int *p = &x;
    x = 6;
    printf("%d ", *p);
    p = &y;
    printf("%d\n", *p);
    return 0;
}
  • A 6 7 Correct
  • B 5 7
  • C 5 5
  • D Compilation error
Correct Answer: Option A

x itself is not const, so x = 6 is fine and *p sees it. "Pointer to const" only forbids writing through p; p itself may also move to y.

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