Why answer is 64 , can anyone help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why answer is 64 , can anyone help me

int main() { int a=320; char *ptr; ptr=(char*)&a; printf ("%d",*ptr); }

24th Jul 2022, 3:45 AM
Always Learn More😎
Always Learn More😎 - avatar
1 Answer
+ 5
320 in decimal equals to 101000000 in binary (it needs 9 bits to fit in) however a char is 8 bits. casting a char from an int gives the lowest byte (least significant 8 bits) which in this case is 01000000 and that's 64 in decimal. the code without using pointers can be written as: printf("%d", (char)320); or int a = 320; printf("%d", (char)a);
24th Jul 2022, 4:45 AM
Tina
Tina - avatar