Sizeof() for floating constant in C
The sizeof() operator in C programming is used to get the size of an object or data type in bytes. However, using sizeof() directly might not yield the desired outcome when dealing with floating-point constants, like float and double in C. The size of the data type or expression to which it is applied is given by the sizeof() operator. When you use sizeof() with a floating-point constant, the associated data type (float or double) will be returned rather than the size of the constant itself. There are many types of floating constant in C.
- Floating Constant:
It has the following syntax:
printf("Size of float constant: %zu bytes\n", sizeof(3.14f));
- Double Constant:
It has the following syntax:
printf("Size of double constant: %zu bytes\n", sizeof(3.14));
float floatConstant = 3.14f;
double doubleConstant = 3.14;
printf("Size of float constant: %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant: %zu bytes\n", sizeof(doubleConstant));
Example 1:
Let us take an example to illustrate the sizeof() operator for floating constant in C.
#include <stdio.h>
int main() {
// Float constant
printf("Size of float constant: %zu bytes\n", sizeof(3.14f));
// Double constant
printf("Size of double constant: %zu bytes\n", sizeof(3.14));
// Using variables
float floatConstant = 3.14f;
double doubleConstant = 3.14;
printf("Size of float constant (via variable): %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant (via variable): %zu bytes\n", sizeof(doubleConstant));
return 0;
}
Output:
Size of float constant: 4 bytes
Size of double constant: 8 bytes
Size of float constant (via variable): 4 bytes
Size of double constant (via variable): 8 bytes
Example 2:
Let us take another example to illustrate the sizeof() operator for floating constant in C.
#include <stdio.h>
int main() {
// Float constant
printf("Size of float constant: %zu bytes\n", sizeof(2.718f));
// Double constant
printf("Size of double constant: %zu bytes\n", sizeof(2.718));
// Using variables
float floatConstant = 2.718f;
double doubleConstant = 2.718;
printf("Size of float constant (via variable): %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant (via variable): %zu bytes\n", sizeof(doubleConstant));
return 0;
}
Output:
Size of float constant: 4 bytes
Size of double constant: 8 bytes
Size of float constant (via variable): 4 bytes
Size of double constant (via variable): 8 bytes
Example 3:
#include <stdio.h>
int main() {
// Float constant without suffix (treated as double)
printf("Size of float constant (no suffix): %zu bytes\n", sizeof(5.555));
// Double constant
printf("Size of double constant: %zu bytes\n", sizeof(5.555));
// Float constant with 'f' suffix
printf("Size of float constant (with 'f' suffix): %zu bytes\n", sizeof(5.555f));
// Using variables
float floatConstant = 5.555f;
double doubleConstant = 5.555;
printf("Size of float constant (via variable): %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant (via variable): %zu bytes\n", sizeof(doubleConstant));
return 0;
}
Output:
Size of float constant (no suffix): 8 bytes
Size of double constant: 8 bytes
Size of float constant (with 'f' suffix): 4 bytes
Size of float constant (via variable): 4 bytes
Size of double constant (via variable): 8 bytes
Example 4:
#include <stdio.h>
int main() {
// Double constant without suffix
printf("Size of double constant (no suffix): %zu bytes\n", sizeof(123.456));
// Float constant with 'f' suffix
printf("Size of float constant (with 'f' suffix): %zu bytes\n", sizeof(123.456f));
// Using variables
float floatConstant = 123.456f;
double doubleConstant = 123.456;
printf("Size of float constant (via variable): %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant (via variable): %zu bytes\n", sizeof(doubleConstant));
return 0;
}
Output:
Size of double constant (no suffix): 8 bytes
Size of float constant (with 'f' suffix): 4 bytes
Size of float constant (via variable): 4 bytes
Size of double constant (via variable): 8 bytes
Explanation:
- As the default representation, we display the size of a double constant without a suffix.
- We show how to explicitly specify a float constant as a float by adding the 'f' suffix.
- Variables are used to demonstrate how to calculate floating-point constant sizes correctly.
Example 5:
Let us take another example to illustrate the sizeof() operator for floating constant in C.
#include <stdio.h>
int main() {
// Double constant without suffix
printf("Size of double constant (no suffix): %zu bytes\n", sizeof(987.654));
// Float constant with 'f' suffix
printf("Size of float constant (with 'f' suffix): %zu bytes\n", sizeof(987.654f));
// Using variables
float floatConstant = 987.654f;
double doubleConstant = 987.654;
printf("Size of float constant (via variable): %zu bytes\n", sizeof(floatConstant));
printf("Size of double constant (via variable): %zu bytes\n", sizeof(doubleConstant));
return 0;
}
Output:
Size of double constant (no suffix): 8 bytes
Size of float constant (with 'f' suffix): 4 bytes
Size of float constant (via variable): 4 bytes
Size of double constant (via variable): 8 bytes
Conclusion:
A data type or object's size, expressed in bytes, can be found using the sizeof() operator in C. When sizeof() is used with constants like float and double returns, it returns the size of the associated data type, not the floating-point constant itself. The size of a double is reflected by applying sizeof() directly to floating-point constants that do not have a suffix because they are, by default, handled as doubles. It's best to use sizeof() with variables containing the constants to get the constant's size precisely. It gives a more realistic picture of the size of the floating-point constants in C since the operator accurately calculates the amount of memory that the variables occupy.