开发者

How does c store a double decimal in an 8 bit slot?

开发者 https://www.devze.com 2023-04-12 05:32 出处:网络
How does c store a double decimal in an 8 bit slot? #include \"stdio.h\" main(){ double x = 123.456; printf(\"\\n %d - %e \\n\",sizeof(x),x);

How does c store a double decimal in an 8 bit slot?

#include "stdio.h"

main(){
  double x = 123.456;
  printf("\n %d - %e \n",sizeof(x),x);
}

outputs:

8 - 23.456

The value of x is correct being开发者_如何学运维 123.456, but the supposedly it is only 8 bits.


That's not 8 bits. It's 8 bytes. And each byte is at least 8 bits (and usually exactly 8 bits).

So it's probably 8 * 8 = 64-bits for a double.

EDIT:

The sizeof() operator yields the size of an object in bytes.

A "byte" is by definition the size of a char. (That's how the C standard defines the word "byte"; it may have different meanings in other contexts.)

The number of bits in a byte is specified by the macro CHAR_BIT, defined in <limits.h>. Almost any system you're likely to encounter will have CHAR_BIT == 8, but I understand that some implementations for DSPs (Digital Signal Processors) have CHAR_BIT set to 16 or 32.


To be horribly pedantic sizeof return the size of the operand in multiples of sizeof char.

On all common platforms that means that sizeof returns in bytes.

0

精彩评论

暂无评论...
验证码 换一张
取 消