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

Difficulty: Medium

Correct Answer: 252

Explanation:


Introduction / Context:
This question presents a small C++ program that defines a struct, initialises its members, and then prints them using a pointer to the struct. Understanding the output requires knowledge of how aggregate initialisation works, how characters are represented using ASCII codes, and how the stream insertion operator prints different types. The code looks simple, but the subtlety lies in interpreting the integer value assigned to the char field and how it appears in the output.


Given Data / Assumptions:

  • The struct sec has two members: an int named a and a char named b.
  • The variable s is initialised as s = {25, 50}, so a receives 25 and b receives the character with code 50.
  • The pointer ps points to s and is used with the arrow operator to access the fields.
  • The line cout << ps->a << ps->b; prints the integer followed by the character with no spacing or newline in between.


Concept / Approach:
In C++, when you initialise a struct with a brace enclosed list, the values are assigned to members in the order of declaration. Thus, s.a becomes 25 and s.b becomes the character whose numeric code is 50. In the ASCII character set, the decimal code 50 corresponds to the character digit 2. The expression ps->a yields the integer 25, and ps->b yields a char with value 50, which is printed as the character 2 by cout. Therefore the stream outputs the characters representing 25 followed by the character 2, all concatenated without any spaces.


Step-by-Step Solution:
Step 1: Identify the struct definition: sec contains int a and char b. Step 2: Apply aggregate initialisation sec s = {25, 50}; which assigns 25 to a and 50 to b. Step 3: Remember that assigning 50 to a char does not store the digits five and zero, but rather the character with ASCII code 50. Step 4: Look up or recall that ASCII code 50 corresponds to the character digit 2. Step 5: Evaluate cout << ps->a << ps->b; which prints the integer 25 as the characters 2 and 5, and then prints b as the character 2, yielding the combined output 252.


Verification / Alternative check:
You can verify this reasoning by writing a small test program that prints static_cast(b) alongside b. Doing so would show that b has the numeric value 50 while displaying as the character 2 when streamed to cout. Since ps->a prints as 25 and ps->b prints as 2, the concatenation in the output stream must be 252. Trying other ASCII codes in similar examples reinforces the idea that cout treats chars as characters, not as their numeric codes, unless you explicitly cast them to an integer type.


Why Other Options Are Wrong:
Option 253: This would imply that the char with code 50 prints as 3, which is incorrect because ASCII code 51 corresponds to the character 3, not 50. Option 254: This would match a scenario where b had ASCII code 52, which corresponds to the character 4, but in the program b is initialised with 50. Option 262: This suggests that b prints as 2 but that the tens digit of a has changed from 2 to 6, which does not match any operation in the code.


Common Pitfalls:
Learners sometimes confuse the numeric literal used in initialisation with the actual characters printed. Assigning 50 to a char does not print 50, it prints the character that has that numeric code in the character set. Another pitfall is to forget that cout uses different overloads of the insertion operator for int and char, so chars are treated as characters by default. To see the numeric value, you must cast the char to int explicitly. Keeping these behaviour details in mind avoids many small bugs and helps in solving questions like this one quickly.


Final Answer:
The program prints the integer 25 followed by the character whose code is 50, which is 2, so the final output is 252.

Discussion & Comments

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