Search code examples
cunsigned-char

when assigning the unsigned char with the integer greater than 255 it gives the different output , why?


#include<stdio.h>
int main()
{
   unsigned char c =292;
   printf("%d\n",c);
   return 0;
}  

The following code gives the output "36". I wanted to know why this happens?


Solution

  • The size of char data_type is 1 byte and its range is 0 to 255. but here initialization is more than 255(i.e. c=292>255)

    Hence, c stores (292-255)th value (i.e. 37th value) and the value c stores is 36(because 0 is the first value).

    It means you have initialize c = 36.

    And finally, printf() func. fetch value from memory and print the value 36.