What will be the output? int main() { char *ptr = "knowledgegate";…
What will be the output?
int main()
{
char *ptr = "knowledgegate";
printf("%c\n", *&*&*ptr);
return 0;
}
Answer: D. k — Concept: The unary operators * (dereference) and & (address-of) are exact inverses of each other. In C, unary operators are right-associative, so a chain like…
- A.
Compiler Error
- B.
Garbage Value
- C.
Runtime Error
- D.
k
Attempted by 328 students.
Show answer & explanation
Correct answer: D
Concept: The unary operators * (dereference) and & (address-of) are exact inverses of each other. In C, unary operators are right-associative, so a chain like *&*&*ptr is grouped as *(&(*(&(*ptr)))) — each adjacent &* or *& pair cancels, and printf's %c conversion always prints exactly one character, never a string, however deep the pointer chain.
Applying it here:
ptr is a char* initialized to the string literal "knowledgegate", so ptr points to its first character, 'k'.
*ptr dereferences ptr and yields the char 'k'.
&*ptr takes the address of that char — this is the same address as ptr itself.
*&*ptr dereferences that address again, giving back 'k'.
&*&*ptr takes the address once more (same address as ptr).
*&*&*ptr dereferences a final time, yielding 'k' — the value printed by %c.
Why not the full string: This chain never touches any character beyond the first: *ptr always yields a single char, not the string, so no number of alternating & and * operations can make it produce "knowledgegate" — printing the whole string would need %s with ptr itself (not a dereferenced char), e.g. printf("%s", ptr).