retagged by
334 views
0 votes
0 votes
i have typed the following code but when i executed it the solution was not according to my expectation.

unsigned short int y= -9;  int iy=y; printf(“%d”,iy);  

solution given by computer is 65527

but i expect the solution to be -9

does unsigned short affecting my solution in any way.
retagged by

1 Answer

0 votes
0 votes

There are multiple things happening here.

“-9” will be scanned by the lexical analyzer and it’ll be treated as an integer constant preceded by minus operator which will return negative of “9”.

This negative of 9 will be stored to the memory address of y. Size of short int is 2 bytes. So, “-9” will be stored using “16” bits and let’s assume a 2’s complement representation. So, $2^{16} – 9 = 65536 – 9 = 65527$ will be stored in the memory location of “y”. 

Now, int iy = y; 

Here, y will return “65527” which will be copied to the memory location of “iy” (for now consider 4 bytes for int). 

So, “%d” or “%u” iy will print “65527” and “%hd” y should print “-9” and “%hu” y should print “65527”.

Related questions

1 votes
1 votes
0 answers
1
0 votes
0 votes
3 answers
2
KrishnaR72 asked Aug 2, 2023
441 views
It is given that m < nlets consider m = 4 and n = 5.If rank(A) = n, this means no of linearly independent columns in A are 5, but we cannot have 5 linearly independent ve...