C Programming

How to Find the Size of float and double in C and C++?

In C and C++, the data types are the most crucial parts and among them, the float and double data types are widely used ones. Though they both are similar in terms of representing real numbers in fractions, however, they have differences in terms of their sizes.

If you are unaware of the size of float and double data types in C and C++, follow this article’s guidelines to learn how to find their sizes.

Before moving towards finding the size of float and double data types, first you must learn about them if you are a beginner.

What are float and double Data Types in C and C++

The float data type is utilized in C and C++ programming to hold floating-point values, which can either be decimal or exponential. You must use the %f specifiers with the float data type variable to output them on the console screen for the case of C programming. While for C++, there are no specifiers for printing the floating-point values.

The double data type has been employed in C and C++ to hold decimal numbers alongside double precision. This is utilized in both C++ and C to specify numerical values which include decimal numbers. A double data type represents an accurate data type capable of storing 64 bits of decimal or floating-point numbers. Because a double appears to have greater precision compared to a float and it consumes twice the memory of the floating-point type. It can quickly hold sixteen to seventeen digits following or preceding a decimal point. In C programming, the double data type variables are printed via %lf specifiers, while in C++, they are simple to be printed using cout.

Find the Size of float and double in C and C++

To find the size of float and double in C and C++, we will use the sizeof function, which is a built-in function in both programming languages.

To find the size of float and double in a C program, follow the below-given code:

#include <stdio.h>

int main()

{

  printf("Size of float=%lu bytes\n", sizeof(float));

  printf("Size of double=%lu bytes", sizeof(double));

  return 0;

}

Output

The above code outputs the size of float and double in C, which is 4 bytes (32Bits) and 8 bytes (64Bits), respectively.

To find the size of float and double in a C++, follow the below-given code:

#include <iostream>

using namespace std;

int main()

{

  float float_Type;

  double double_Type;

  cout << "Size of float= " <<

  sizeof(float_Type) <<" bytes\n";
 
  cout << "Size of double= " <<

  sizeof(double_Type) <<" bytes \n";

  return 0;

}

Output

The above code output confirms the size of float and double in C++ is 4 bytes (32Bits) and 8 bytes (64Bits), respectively like C.

Conclusion

float and double are widely used datatypes in C and C++ and they both represent real numbers in fractions. However, they are different in size. The size of float and double in C and C++ is 4 bytes (32Bits) and 8 bytes (64Bits), respectively, which can be found through the sizeof function discussed in the above-mentioned guidelines.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.