C float Data Type

In C, the float data type is used to store decimal numbers (floating-point numbers). It is commonly used when precision is needed but memory efficiency is also a concern. A float can store real numbers with fractional parts.

The float data type is widely used in C programming for scientific calculations, mathematical operations, and when precision is necessary.


1. Storage Size of float Data Type

The storage size of a float is typically 4 bytes, though this may vary based on system architecture and compiler implementation.

TypeStorage Size
float4 bytes

2. Values Stored by float Data Type

The float data type stores real numbers, including both positive and negative values, with decimal points. However, since it uses a limited number of bits, it has a certain degree of precision and may not store exact values for very large or very small numbers.

Example values that can be stored in float:

3.14, -0.005, 12345.6789, -987.65

float in C follows the IEEE 754 single-precision floating-point format, which consists of:

  • 1-bit sign
  • 8-bit exponent
  • 23-bit mantissa (fraction)
Sign (1 bit)Exponent (8 bits)Mantissa (23 bits)

Value=(−1)Sign×(1.Mantissa) × 2Exponent−127

3. Example: Declaring and Using float Variables

Let’s see a simple program demonstrating how to declare and use float variables in C.

main.c

</>
Copy
#include <stdio.h>

int main() {
    float num1 = 3.14;
    float num2 = -7.5;
    float sum = num1 + num2;

    printf("Number 1: %.2f\n", num1);
    printf("Number 2: %.2f\n", num2);
    printf("Sum: %.2f\n", sum);

    return 0;
}

Explanation:

  1. We declare three float variables: num1, num2, and sum.
  2. We assign values 3.14 and -7.5 to num1 and num2 respectively.
  3. The sum of these two numbers is stored in the sum variable.
  4. We use printf() with the %.2f format specifier to print the float values up to two decimal places.

Output:

Number 1: 3.14
Number 2: -7.50
Sum: -4.36

4. Checking Storage Size of float Programmatically

We can determine the storage size of float in bytes using the sizeof operator.

main.c

</>
Copy
#include <stdio.h>

int main() {
    printf("Size of float: %lu bytes\n", sizeof(float));
    return 0;
}

Output (varies based on system architecture):

Size of float: 4 bytes

5. Minimum and Maximum Values of float

The range of values a float can store is much larger than an int but with limited precision.

Storage SizeMinimum ValueMaximum Value
4 bytes~1.2 × 10-38~3.4 × 1038

6. Getting Maximum and Minimum Values of float Programmatically

The maximum and minimum values of a float can be retrieved using float.h.

main.c

</>
Copy
#include <stdio.h>
#include <float.h>

int main() {
    printf("Minimum float value: %E\n", FLT_MIN);
    printf("Maximum float value: %E\n", FLT_MAX);
    return 0;
}

Output:

Minimum float value: 1.175494E-38
Maximum float value: 3.402823E+38

Conclusion

In this tutorial, we explored the float data type in C, including:

  1. Its ability to store decimal numbers with a precision of up to 6-7 decimal digits.
  2. Its typical storage size of 4 bytes.
  3. How to get the storage size programmatically using sizeof().
  4. The minimum and maximum values that float can store.
  5. How to retrieve these values using float.h.