C Programming :: Declarations and Initializations - Discussion
65 / 54
#include <stdio.h>
void main()
{
unsigned short a=-1;
unsigned char b=a;
printf("%d %d ",a,b);
}
What is output of the program?
#include <stdio.h>
void main()
{
unsigned short a=-1;
unsigned char b=a;
printf("%d %d ",a,b);
}
What is output of the program?
A65535 -1
B65535 65535
C65535 255
D-1 -1
ENone of above
Show Explanation
Clearly, a cannot be assigned with -1, as -1 is not in the range of unsigned short. So, when we try to assign -1 to a, automatically 65535 gets assigned to a, as it follows a cyclic pattern.
Same is the result when we try to assign -1 to b, which is an unsigned char. The value range for unsigned char is from 0 to 255. So, automatically, 255 gets assigned to b.
Hence, the output is 65535 255.
Asked In ::
The value range for unsigned short is from 0 to 65,535.
Clearly, a cannot be assigned with -1, as -1 is not in the range of unsigned short. So, when we try to assign -1 to a, automatically 65535 gets assigned to a, as it follows a cyclic pattern.
Same is the result when we try to assign -1 to b, which is an unsigned char. The value range for unsigned char is from 0 to 255. So, automatically, 255 gets assigned to b.
Hence, the output is 65535 255.
Read Full Answer
Report Error
Please Login First Click Here