#include <iostream> int main(int argc, char **argv) { std::cout << 25u - 50; return 0; } | SoloLearn: Learn to code for FREE!

0

#include <iostream> int main(int argc, char **argv) { std::cout << 25u - 50; return 0; }

What will the line of code below print out and why? #include <iostream> int main(int argc, char **argv) { std::cout << 25u - 50; return 0; }

c++

12/15/2017 9:35:47 AM

nirav malaviya

5 Answers

New Answer

+2

Have you tried in your code playground? What does it outputs?

+1

Yes. When the ligne "cout << 25u - 50" is executed, the calculation "25u-50" is proceeded first, and by default, the result is stored in an unsigned int. So the result is max_int-25 = 4294967271. If you wish to outpout '-25', you should first store the result of the calculation in a signed int, then print it. Like this : #include <iostream> using namespace std; int main() { signed int res=25u-50; cout << res; return 0; }

+1

Or simply : int main() { int res=25u-50; cout res; return 0 ; }

0

The answer is not -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32 bit integers.