In C language, we have three floating data types i.e. float, double and long double. And the exact size of each of these 3 types depends on the C compiler implementation/platform. The following program can be used to find out the size of each floating data type on your machine.
But what about the size of a floating point constant (e.g. 31.4 or 2.718)? For example if we have PI macro defined as follows, what would be the sizeof(3.14).
#define PI 3.14
Now if we do sizeof(PI), what will be its size? Is is equal to sizeof(float) ? Or is it also compiler implementation dependent. Well, for floating constants, C standard (C11 i.e. ISO/IEC 9899:2011) has given guideline. As per C11 clause 22.214.171.124, “An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.“
It means the type of a floating constant is same as that of double data type. So if size of a double on a machine is 8 bytes, the size of floating constant would be 8 bytes. One can find out this using the below program
As per the above mentioned C standard clause, a floating constant can be converted to float type by using f or F. Similarly, a floating constant can be converted to long double by using l or L. So it shouldn’t take much thought on guessing the output of the following:
Please do Like/Tweet/G+1 if you find the above useful. Also, please do leave us comment for further clarification or info. We would love to help and learn 🙂
- Is sizeof for a struct equal to the sum of sizeof of each member?
- sizeof operator in C
- Implement Your Own sizeof
- Why does sizeof(x++) not increment x in C?
- Do not use sizeof for array parameters
- Anything written in sizeof() is never executed in C
- Operands for sizeof operator
- How to find size of array in C/C++ without using sizeof ?
- Difference between strlen() and sizeof() for string in C
- C Program to Multiply two Floating Point Numbers
- Floating Point Operations & Associativity in C, C++ and Java
- Draw a circle without floating point arithmetic
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Rounding Floating Point Number To two Decimal Places in C and C++