Consider the following C++ program involving a structure and pointer: #include <iostream> using namespace std; struct sec { int a; char b; }; int main() { struct sec s = {25, 50}; struct sec *ps = (struct sec *)&s; cout << ps->a << ps->b; return 0; } What is the output of this program when it is run?

Difficulty: Medium

Correct Answer: 252

Explanation:


Introduction / Context:
This question examines how C++ initializes structure members and how char values are printed with cout. It is a small but instructive example about integer to character conversion using ASCII codes and about member access through structure pointers. Understanding this behaviour is important when debugging low level code and when mixing integer constants with character types.


Given Data / Assumptions:

  • Structure sec has an int member a and a char member b.
  • The structure is initialized as s = {25, 50}; where 50 is an int literal assigned to a char.
  • A pointer ps points to s, and cout prints ps->a followed by ps->b with no space.


Concept / Approach:
When assigning 50 to a char, C++ converts the integer to the corresponding character code according to the implementation character set, typically ASCII. In ASCII, the decimal code 50 represents the character '2'. Therefore, b holds the character '2'. The expression ps->a prints 25 as a decimal integer, and ps->b prints the character '2'. Concatenating them with cout produces the string "252". No extra space or newline is added.


Step-by-Step Solution:
Step 1: Initialize s so that s.a = 25 and s.b is assigned the integer 50 converted to char. Step 2: In the ASCII character set, code 50 corresponds to the character '2', so s.b is effectively '2'. Step 3: ps is a pointer to s, so ps->a refers to s.a and ps->b refers to s.b. Step 4: cout << ps->a prints the integer 25 as characters '2' and '5'. Step 5: cout then appends ps->b, which is the character '2', yielding the output "252".


Verification / Alternative check:
You can verify this behaviour by inserting a space in the middle, such as cout << ps->a << ' ' << (int)ps->b;, which will print "25 50". Casting ps->b to int reveals the underlying numeric ASCII code 50. Without the cast, cout treats b as a character and prints '2', confirming that 25 and the character with code 50 combine to form "252".


Why Other Options Are Wrong:
Option B (253), option C (254), and option D (262) all assume different combinations of digits or misinterpret the character code. None of them matches the exact concatenation of the integer 25 and the character '2'. Only "252" correctly reflects the integer part and the ASCII interpretation of the char value 50.


Common Pitfalls:
A common mistake is to assume that initializing a char with 50 will store the digit 5 or some two digit string. In reality, a char holds a single byte that corresponds to a single character code. Another pitfall is forgetting that cout treats char as a character rather than as a small integer, so it prints a symbol instead of a number. When in doubt, explicit casting to int can make debugging output clearer.


Final Answer:
The program prints the characters "252", so the correct option is 252.

Discussion & Comments

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