+ 4

Why this code prints 44 ?

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

29th Jan 2019, 7:57 AM
Mihai C
Mihai C - avatar
3 Respuestas
+ 18
unsigned char is between [0, 255] 300 is bigger than 255 so it throws away extra bits and you get 300 % 256 = 44
29th Jan 2019, 8:17 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
unsigned char are 1 byte (8 bits) long... But to represent 300 9 bits are needed, so there is no room in x for the 9th bit If you use binary or hexa: 300 is: 1 0010 1100 12C When assigned to unsigned char bit 8 (9th bit) is discarded, so: x = 0010 1100 2C 2*16 + 12 = 44 :)
29th Jan 2019, 2:28 PM
unChabon
unChabon - avatar
+ 2
good answer diego Code
29th Jan 2019, 3:21 PM
Rick Shiffman
Rick Shiffman - avatar