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 5 — Concept: A pointer variable stores the memory address of another variable, and the unary * operator dereferences a pointer — it follows the address the…
- A.
5 5 5
- B.
5 5 junk
- C.
5 junk junk
- 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.
int k = 5;declares an int objectkholding the value 5.int *p = &k;declarespas a pointer-to-int and initializes it withk’s address, sopholds&k.int **m = &p;declaresmas a pointer-to-pointer-to-int and initializes it withp’s address, somholds&p.Evaluating
*pfollows the addresspholds (&k) and reads the int stored there: 5.Evaluating
**mfirst followsmtop’s stored value (&k, sincemholds&p), then dereferences again to follow that same address tok: also 5.printf("%d %d %d",k,*p,**m);therefore printsk,*pand**m— three separate reads of the same underlying int object — giving the output5 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