Addresses of string literals and %s printing: given that the memory address of the literal 'Hello1' is 1022 (Turbo C, 16-bit DOS), what will this print? #include<stdio.h> int main() { printf('%u %s ', &'Hello1', &'Hello2'); return 0; }

Difficulty: Medium

Correct Answer: 1022 Hello2

Explanation:


Introduction / Context:
This question examines pointer types with string literals, how the unary & operator changes the type, and how printf interprets arguments for %u and %s. It also assumes a specific address (1022) for the literal 'Hello1' to make the numeric output deterministic.


Given Data / Assumptions:

  • &'Hello1' yields a pointer to the entire array object of the literal (type char ()[7]), whose value as an address equals the start address of the first character.
  • &'Hello2' similarly yields a pointer to the array for 'Hello2'.
  • printf with %u expects an unsigned int; the pointer is passed and converted as an integer value on this legacy platform.
  • %s expects a char, and &'Hello2' points to the same start address as a char*, so it prints 'Hello2'.


Concept / Approach:
Although the pointer types are not char*, the numeric value of &'Hello1' prints as the base address. For %s, &'Hello2' is treated as the base address of the literal string. On many older compilers, this prints correctly despite the subtle type mismatch (on modern compilers, you would cast explicitly).


Step-by-Step Solution:

%u with &'Hello1' → prints its address → 1022 (as given).%s with &'Hello2' → prints the characters from that base address → 'Hello2'.


Verification / Alternative check:
Replacing &'Hello2' with 'Hello2' (which decays to char*) still prints 'Hello2'. Both point to the same base address.


Why Other Options Are Wrong:

'Hello1 1022' or 'Hello1 Hello2': would require using %s with 'Hello1' and not printing the numeric address.'1022 1022': incorrectly assumes both conversions produce numeric printing.'Error': legacy compilers typically accept this; behavior shown is the intended trick.


Common Pitfalls:
Confusing &'literal' with the decayed char* of a literal; assuming %s requires exactly char* and not recognizing platform permissiveness in old toolchains.


Final Answer:
1022 Hello2.

More Questions from Strings

Discussion & Comments

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