sizeof float (3.0) vs (3.0f)

瘦欲@ 提交于 2019-11-30 02:38:03

问题


What is the difference between sizeof(3.0) and sizeof(3.0f)

I was expecting both of them to give the same result (sizeof float)..but its different.

In 32 bit machine,gcc compiler, sizeof(3.0f) =>4 sizeof(3.0) => 8

Why so?


回答1:


Because 3.0 is a double. See C syntax Floating point types.

Floating-point constants may be written in decimal notation, e.g. 1.23. Scientific notation may be used by adding e or E followed by a decimal exponent, e.g. 1.23e2 (which has the value 123). Either a decimal point or an exponent is required (otherwise, the number is an integer constant). C99 introduced hexadecimal floating-point constants, which follow similar rules except that they must be prefixed by 0x and use p to specify a hexadecimal exponent. Both decimal and hexadecimal floating-point constants may be suffixed by f or F to indicate a constant of type float, by l or L to indicate type long double, or left unsuffixed for a double constant.




回答2:


  • 3.0f is float (4 bytes)
  • 3.0 is double (8 bytes)

more info




回答3:


3.0 is a double, not a float.

doubles are twice as wide as floats.

EDIT: 3.0d is only in C#



来源:https://stackoverflow.com/questions/1355379/sizeof-float-3-0-vs-3-0f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!