+ 17
C++ challenge question from my declined list: Guess the output
Another interesting question. No code playground! Try it for yourselves. Brace yourselves: What is the output of the following code? Assume 32 bit integers. cout << 25u - 50; a. -25 b. 25 c. 4294967271 d. Error Best answer to whoever gets the correct answer and explains why the answer is correct.
14 Answers
+ 26
c. 4294967271
In C++, if the datatypes of two values used in calculation is different from each other, the value with datatype of lower size hierarchy will be converted to the datatype of the other value which has the higher size hierarchy.
Size hierarchy : short int < unsigned short int < int < unsigned int < long int < unsigned long int
Size hierarchy for floating point numbers is : float < double < long double
In the question, 25u is an unsigned int. 50 is an int. So according to the above information, 50 will be converted from int to unsigned int. Hence, 25u - 50u is computed and the result of 4294967271 is obtained.
// I'm not sure how the conversion works though. What I am sure of was that the answer would neither be a, b or d.
// would have needed this: https://www.quora.com/How-do-I-convert-an-integer-into-an-unsigned-integer-in-C
+ 14
Well done @Hatsy Rei. The key to understanding this question was the u means unsigned. In C++, if the types of two operands differ from one another, then the operand with the "lower type" will be promoted to the type of the "higher type" operands. unsigned has a higher order than signed thus the 50 becomes 50u.
Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.
+ 10
This was covered in the c++ course btw, just not in great detail. I was asked this question before my very first internship quite awhile ago, and I worked it out in this manner.
+ 7
If you know the answer and can explain why, share mate 👍
+ 6
Any particular reason for picking a @Sumita?
+ 5
@Hatsy rei
I got a headache just reading that
+ 4
Crap. I know the answer but I can't share. No, I didn't use CP, but I've ran a code like this before. I'll give a hint though, the u defines what type of integer 25 is and this is important to the Mathematical operation being performed.
+ 4
@Hasty Rei is correct though.
+ 4
*mindblown*
Nice question, I wish that was excepted.
I am going have to review that.
+ 3
what is u here ,is it for unsigned
+ 3
@Sumita Will the final answer be signed or unsigned if one integer is an unsigned?
+ 3
@Ace I bet it was downvoted because people did not get it.
It is a kata level question
I like it.
If I came accross it.
I would skip it, until I compiled it.
+ 1
C for C program
I did this
https://code.sololearn.com/cKFkP4blfqt7/?ref=app
+ 1
Wow
@Hatsy what can you do with the data type hierarchy?
Hot today
Python — File Handling
2 Votes
Help me
0 Votes
Question is write a c program to print prime numbers up to n and print the largest number in array.
1 Votes
What’s wrong?
2 Votes
Achievements on Sololearn
1 Votes
Queue & dequeued
0 Votes