What will be the output of the following C code? void main() { int k = 4; int…
What will be the output of the following C code?
void main()
{
int k = 4;
int *const p = &k;
int r = 3;
p = &r;
printf("%d", p);
}Answer: C. Compile time error — In C, the declaration int *const p creates a constant pointer to an int: the pointer variable itself is fixed once initialized, though the value it points to…
- A.
Address of k
- B.
Address of r
- C.
Compile time error
- D.
Address of k + address of r
Attempted by 78 students.
Show answer & explanation
Correct answer: C
In C, the declaration int *const p creates a constant pointer to an int: the pointer variable itself is fixed once initialized, though the value it points to can still be changed through it. This is the opposite of const int *p, where the pointee is read-only but the pointer can be redirected. Once a constant pointer is initialized, any later attempt to reassign it (p = ...;) is invalid and the compiler rejects it as an error.
int k = 4; declares an int variable k initialized to 4.
int *const p = &k; declares p as a constant pointer to int, initialized to point at k. From this point onward, p itself can never be reassigned.
int r = 3; declares another int variable r.
p = &r; attempts to reassign the constant pointer p to point at r instead of k — this is disallowed because p is const.
The compiler rejects this reassignment with an “assignment of read-only variable ‘p’” error, so the program fails to compile.
Because compilation fails, execution never reaches the printf statement — there is no runtime output at all, only a compile-time error.
This matches the actual compiler diagnostic for this program — error: assignment of read-only variable ‘p’ at the line p = &r; — confirming the failure happens at compile time, not at runtime, which is exactly why “Compile time error” is the outcome rather than any printed address.