- 2

#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; }

15th Dec 2017, 9:35 AM
nirav malaviya
nirav malaviya - avatar
5 odpowiedzi
+ 2
Have you tried in your code playground? What does it outputs?
15th Dec 2017, 9:49 AM
Corentin
Corentin - avatar
+ 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; }
15th Dec 2017, 10:18 AM
Corentin
Corentin - avatar
+ 1
Or simply : int main() { int res=25u-50; cout res; return 0 ; }
15th Dec 2017, 10:42 AM
Corentin
Corentin - avatar
0
The answer is not -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32 bit integers.
15th Dec 2017, 9:53 AM
nirav malaviya
nirav malaviya - avatar
0
Using C++ Write a program that populates a LIST with random integers from 10-60, then calls a function that divides the list into two (2) Lists. The function then should remove all values greater that 19 from the list and store them in a NEW list
6th Jul 2022, 7:36 AM
Isaac Sule