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 errorIn 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…

  1. A.

    Address of k

  2. B.

    Address of r

  3. C.

    Compile time error

  4. 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.

  1. int k = 4; declares an int variable k initialized to 4.

  2. 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.

  3. int r = 3; declares another int variable r.

  4. p = &r; attempts to reassign the constant pointer p to point at r instead of k — this is disallowed because p is const.

  5. The compiler rejects this reassignment with an “assignment of read-only variable ‘p’” error, so the program fails to compile.

  6. 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.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…