IGDTUW 2026 Computer PYQ — What is the output of the following C++ program? #include <iostre… | Mathem Solvex | Mathem Solvex
Tip:A–D to answerE for explanationV for videoS to reveal answer
IGDTUW 2026 — Computer PYQ
IGDTUW | Computer | 2026
What is the output of the following C++ program?
#include <iostream>
using namespace std;
int main() {
unsigned char x = 255;
x++;
cout << int(x);
return 0;
}
Choose the correct answer:
A.
255
B.
256
C.
0
(Correct Answer)
D.
Compilation Error
Correct Answer:
0
Explanation
Step-by-Step Explanation:
Data Type Range:
An unsigned char in C++ typical occupies 1 byte (8 bits) of memory. The range of values it can store is from 0 to 28−1, which equals:
0 to 255
Initialization:
The variable x is initialized to its maximum possible value:
x=255
Increment and Wrap-around (Overflow):
When the post-increment operator x++ is executed, 1 is added to 255.
Since an unsigned integer type cannot exceed its maximum limit (255), it safely overflows and wraps around to the beginning of its range (modulo arithmetic, i.e., (255+1)(mod256)):
255+1→0
Type Casting and Output:
The code int(x) explicitly type-casts the character value to an integer so that cout prints it as a numeric value instead of an ASCII character code. Since the wrapped-around value of x is 0, the final output is:
0
Explanation
Step-by-Step Explanation:
Data Type Range:
An unsigned char in C++ typical occupies 1 byte (8 bits) of memory. The range of values it can store is from 0 to 28−1, which equals:
0 to 255
Initialization:
The variable x is initialized to its maximum possible value:
x=255
Increment and Wrap-around (Overflow):
When the post-increment operator x++ is executed, 1 is added to 255.
Since an unsigned integer type cannot exceed its maximum limit (255), it safely overflows and wraps around to the beginning of its range (modulo arithmetic, i.e., (255+1)(mod256)):
255+1→0
Type Casting and Output:
The code int(x) explicitly type-casts the character value to an integer so that cout prints it as a numeric value instead of an ASCII character code. Since the wrapped-around value of x is 0, the final output is: