Quiz on programming fundamentals-1

In the quiz there was a question-
"/* Output ? */
#include
using namespace std;

int main(){
long signed CodingBlocks = 2017;
short unsigned BOSS1 = -2018;
unsigned BOSS2 = -2019;
int BOSS3 = -2020;
long long unsigned BOSS4 = -2021;
short unsigned Nagarro = 2018.9;
long signed HackerBlocks = ‘A’;
cout<<CodingBlocks<<endl;
cout<<BOSS1<<endl<<BOSS2<<endl<<BOSS3<<endl<<BOSS4<<endl;
cout<<Nagarro<<endl;
cout<<HackerBlocks<<endl;

return 0;
}"

Here, only " long signed", “short unsigned”…e.t.c is mentioned.
Should it not be "long signed int ".
The point is that the data type has not been mentioned next to signed,unsigned…
Also, please explain the answer as well. We have to predict output. Couldnt understand that as well.
Thanks

Hey Divyam, when we mention signed or unsigned in c++ by default it is taken as signed int and unsigned int data type and in the output just the values assigned to the variables are printed.

1 Like

The answer is
2017 2^16 - 2018 2^32 -2019 -2020 2^64 - 2021 2018 65

Why did 2^16, 2^32, 2^64 come here? What does that signify? How would we judge this output?

Hey Divyam, output of this code is
2017
63518
4294965277
-2020
18446744073709549595
2018
65

and you can check by running this code on online ide.

1 Like

But why did it show values like 63518,4294965277…e.t.c. Whats the reason for it ?

Hey Divyam, that’s because you have assigned -ve values to unsigned type variables, so 2’s compliment of those values is assigned to the variables. For eg. here BOSS2 is assigned the bit pattern representing -2019 (in 2’s complement) to the unsigned int. Which is a large unsigned value. Its 2^32 - 2019 or 4294967296 - 2019 = 4294965277.

1 Like