Consider the C program: main() { char *p; p = "Hello"; printf("%c ", *&*p); } What will be the output when this program is executed?

Difficulty: Medium

Correct Answer: H

Explanation:


Introduction / Context:
This question examines your understanding of pointer dereferencing and the address operator in C. The expression *&*p may look confusing at first glance, but it is built from basic operations that you should be able to simplify step by step. The program uses a pointer to a string literal and then applies these operators in a nested way.


Given Data / Assumptions:

  • The pointer p points to the first character of the string literal "Hello".
  • The expression *p gives the character at the location to which p points.
  • The address operator and the dereference operator are applied in sequence as *&*p.


Concept / Approach:
In C, the dereference operator * and the address operator ampersand are inverse operations when applied to valid objects. If x is an object, then ampersand x gives its address, and *ampersand x yields x again. In this program, *p is the character at the location pointed to by p. The expression &*p therefore takes the address of that character, which is effectively the same as p because p already points to that character. Applying * again, *&*p becomes *p, which is the first character of the string literal. So the expression inside printf simply yields the character H.


Step-by-Step Solution:
Step 1: The pointer p is assigned the address of the first character of the string literal "Hello".Step 2: The expression *p dereferences p and yields the first character H.Step 3: The expression &*p takes the address of that character. Since p already points to that character, &*p is equal to p.Step 4: The final expression *&*p is therefore the same as *p, which is H.Step 5: The printf call uses the %c format specifier, so it prints the single character H followed by a newline.


Verification / Alternative check:
You can simplify *&*p algebraically by treating &* as an identity when applied to a valid lvalue. Many C references explain that if p is a valid pointer, then *p is an lvalue and &*p equals p. Testing the program in a C compiler produces H as output, which confirms the reasoning.


Why Other Options Are Wrong:
Option B suggests that the entire string Hello is printed, but the format specifier %c prints only a single character, not a whole string.Option C claims a compilation error, but the syntax is valid in standard C.Option D indicates that each character is printed separately with spaces, which is not what this single printf call does.


Common Pitfalls:
Students sometimes overcomplicate expressions involving * and ampersand, forgetting that they are inverse operations when used in pairs. Another pitfall is to confuse %c with %s in printf. The former prints one character, while the latter expects a pointer to a character array that ends with a zero byte. Always check the format specifier when predicting output.


Final Answer:
The correct answer is H.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion