Multiple choice technology programming languages

Consider the following declarations: int a[10]; int *p=a; Which among the following is portable way to print value of pointer p?

  1. printf("%d\n",p);

  2. printf("%u\n",p);

  3. printf("%lu",p);

  4. printf("%p\n",(void*)p);

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The %p format specifier is the portable, standard way to print pointer values in C. It requires a void* argument, so (void*) cast ensures proper conversion. Options A, B, and C use incorrect format specifiers - %d expects int, %u expects unsigned int, and %lu expects unsigned long, none of which are guaranteed to correctly represent a pointer on all platforms.