B
bob
Earlier in this group I posted:
***************************************************************************
I can't get 'number' to print like I think it should:
int main()
{
int number;
printf("two digit number please:\n");
fread(&number, sizeof(int), 1, stdin);
printf("your number is: %d\n", number);
return 0;
}
***************************************************************************
and Keiths response was:
When I tried this program myself, I entered "45" followed by a
newline, then I had to enter another newline to make a total of 4
input characters (sizeof(int) is 4 on my system). The value stored in
"number" was 168441140, the result of storing the integer values of
'4', '5', '\n', and '\n' into a 4-byte integer. Your results might
differ, but see if you can figure out why you got the result you did.
******************************************************************************************
So I tried to figure out what '4' '5' \n \n would be in hex, binary,
and decimal. I got
'4' = 00110100 0x34
'5' = 00110101 0x35
\n = 00000110 0x0a
so that pasted together should be: 0x34350a0a = 875891210
00110100,00110101,00000110,00000110 = 875890182
( I put commas in to
make it less painful to look at)
I used this calculator
(http://www.microcontroller.com/Embedded.asp?did=92) for the binary to
decimal and hex to decimal conversion.
Can someone tell me where I went wrong? Thanks
***************************************************************************
I can't get 'number' to print like I think it should:
int main()
{
int number;
printf("two digit number please:\n");
fread(&number, sizeof(int), 1, stdin);
printf("your number is: %d\n", number);
return 0;
}
***************************************************************************
and Keiths response was:
When I tried this program myself, I entered "45" followed by a
newline, then I had to enter another newline to make a total of 4
input characters (sizeof(int) is 4 on my system). The value stored in
"number" was 168441140, the result of storing the integer values of
'4', '5', '\n', and '\n' into a 4-byte integer. Your results might
differ, but see if you can figure out why you got the result you did.
******************************************************************************************
So I tried to figure out what '4' '5' \n \n would be in hex, binary,
and decimal. I got
'4' = 00110100 0x34
'5' = 00110101 0x35
\n = 00000110 0x0a
so that pasted together should be: 0x34350a0a = 875891210
00110100,00110101,00000110,00000110 = 875890182
( I put commas in to
make it less painful to look at)
I used this calculator
(http://www.microcontroller.com/Embedded.asp?did=92) for the binary to
decimal and hex to decimal conversion.
Can someone tell me where I went wrong? Thanks