+ 21
Can anyone explain how the output is 64
10 Answers
+ 8
If I am not mistaken, this is from one of the C challenge quizzes. As Gordie explained, the answer can vary depending on the machine/processor architecture, so this is not such a great question for a quiz as the answer may vary from one platform to another.
+ 6
Arjun Rawte thanks for expalantion
+ 6
Char has 1 byte of memory, it's range is from -128 to 127
If u assign a value
char ch=128
128 is not it's domain, it's value is 1 more than the Max value of char i.e 127 so circularly it holds -128 instead of storing 128, similarly one more example,
char ch=130;
Now ch holds -126
Similarly if u want to store 320
Circularly it holds 64
+ 1
There's a minor incompatibility regarding type punning in the provided code that should be addressed. When trying to reinterpret a pointer to either `char *` or `void *` it should be considered that explicit conversion has to be provided.
char *ptr = (char *)&a;
The above assignment statement "without" explicit conversion might run without any problem (only a warning) in a standard C compiler, yet any attempt to compile the code in C++ ending up with an error.
Also, the endianness of a machine (and the assumption made by Gordie) can be easily checked by simple pointer arithmetic as follow
#include <stdio.h>
int main() {
int a = 320;
char *ptr = (char *)&a;
size_t size = sizeof(int);
while (size--) {
printf ("%#x ", *ptr);
++ptr;
}
return 0;
}
Output: 0x40 0x1 0 0
+ 1
Because signed char range lies between-128 to 127 .
If we give value more than 127 it will cross it's limit and it will come to -128 ,-127,-126 to 0 and again Move towards positive value 127.eg a=320 i.e 320-256. Because if we give a=256 it will give 0 .and any value more than 256 it will get subtract from that value ,and give +ve value up to 383 i.e (256+127) after 383 it will again Move towards-128 to 0.and so on,
0
Because char is only of 8 bits so, it's take only 8 bits from right.
- 2
The address of the variable " a" is 64 bro
- 3
Hhhhhh