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 126.96.36.199, “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 🙂
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Is sizeof for a struct equal to the sum of sizeof of each member?
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Implement Your Own sizeof
- Operands for sizeof operator
- Do not use sizeof for array parameters
- How to find size of array in C/C++ without using sizeof ?
- sizeof operator in C
- Difference between strlen() and sizeof() for string in C
- Anything written in sizeof() is never executed in C
- Why does sizeof(x++) not increment x in C?
- Write a one line C function to round floating point numbers
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Floating Point Operations & Associativity in C, C++ and Java
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Draw a circle without floating point arithmetic
- Rounding Floating Point Number To two Decimal Places in C and C++
- C Program to Multiply two Floating Point Numbers
- Problem in comparing Floating point numbers and how to compare them correctly?
- How to print range of basic data types without any library function and constant in C?