C Programming MCQ

1:

The following program fragment 

    unsigned i = 1;
     int j = -4;
     printf ("%u ", i + j);


prints

A.

garbage

B.

-3

C.

an integer that changes from machine to machine

D.

none of above

 

Answer : C

Explanation :

In the computer I used to execute this program. the output was 4294967293. That's because in my system,
sizeof (int ) is 4 bytes (32 bits), and negative numbers are represented in 2's complement form. This means -4 will be represented as 11111111 11111111 11111111 11111100 (i.e. 30 one's followed by 2 zeroes). Note that this number is 232 - 1 - 3. Before j gets added to 1, it will be converted to an unsigned integer. So, i + j is essentially adding 1 to 232- 1 - 3 which gives 4294967293.

Write your comments here:


Report Error
 

Option: A

Explanation : Explanation will come here. Explanation will come here. Explanation will come here. Explanation will come here. Explanation will come here.