What is the output of this C code? #include <stdio.h> void main() { int k=5;…

2016

What is the output of this C code?

#include <stdio.h>
void main()
{
int k=5;
int *p=&k;
int **m=&p;
printf("%d %d %d",k,*p,**m);
}

Answer: A. 5 5 5Concept: A pointer variable stores the memory address of another variable, and the unary * operator dereferences a pointer — it follows the address the…

  1. A.

    5 5 5

  2. B.

    5 5 junk

  3. C.

    5 junk junk

  4. D.

    Compile time error

Attempted by 707 students.

Show answer & explanation

Correct answer: A

Concept: A pointer variable stores the memory address of another variable, and the unary * operator dereferences a pointer — it follows the address the pointer holds to read (or write) the object stored there. Pointers can be chained: a pointer-to-pointer stores the address of another pointer, so applying * twice follows two addresses in sequence to reach the final underlying object.

  1. int k = 5; declares an int object k holding the value 5.

  2. int *p = &k; declares p as a pointer-to-int and initializes it with k’s address, so p holds &k.

  3. int **m = &p; declares m as a pointer-to-pointer-to-int and initializes it with p’s address, so m holds &p.

  4. Evaluating *p follows the address p holds (&k) and reads the int stored there: 5.

  5. Evaluating **m first follows m to p’s stored value (&k, since m holds &p), then dereferences again to follow that same address to k: also 5.

  6. printf("%d %d %d",k,*p,**m); therefore prints k, *p and **m — three separate reads of the same underlying int object — giving the output 5 5 5.

Cross-check: Since p and m both ultimately resolve (through one and two dereferences respectively) to the same address as k, any assignment made through *p or **m would also change k itself. No such assignment happens here, so all three reads simply return k’s unmodified value.

Note on the reported void main() signature: it is non-conforming to the C standard, which specifies int main(). In practice, common compilers used in introductory C teaching (e.g. GCC without strict/-Werror flags, or older Turbo C) accept it with at most a warning rather than a hard error, so it does not by itself force a compile-time failure for this exercise. This item is Indian Space Research Organisation (ISRO) Scientist/Engineer ‘SC’ Computer Science 2016, Q20 (Booklet A); the exam’s own official answer key marks this option (‘5 5 5’) as correct, consistent with the pointer-dereference derivation above.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…