  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Float in C

Last Updated on March 28, 2023 by Prepbytes C is one of the most commonly used programming languages around the world. The C language provides various data types and in these data types, we can store the data in various formats. Some of the types of data we can store are a string, integer, boolean, etc. One of the most commonly used datatypes is Float in C. While moving further in this article we will learn all about float in c followed by its uses, examples, and the difference between float and double.

## What is Float in C?

Float in a C programming language can be referred to as a data type that is used to store the floating point numbers. Float in C has a memory size of 4 bytes i.e, 32 bits. We can use the float precisely upto 7 digits after that the float in c will give a garbage value. If we want to store a number beyond the range of float in c we can use the double.

### Syntax of Float in C

The syntax of float in c is given below:

``float variable_name=val;``

You can also assign multiple variables together by using a single float by continuing them in the declaration statement.

### Parameters of Float in C

There are certain parameters of float in c.

• Varible_name: It is the name of the variable that you will assign to the float type variable, with this name the variable is called in the entire program.
• Val: It is the value that you assign to the variable.

### Double vs Float in C

Both double and float are the two data types that are used to take and return the numbers in decimals. Here we will discuss the difference between both them so that you know which data type is best for you to use in various scenarios.

• Precision is the primary distinction between float and double. The variable value’s level of correctness is determined by precision. Double type accuracy is two times greater than float data type precision. In simpler words, it indicates that double, double precision is used to initialize the variable rather than the float data type.
• A double data type needs twice as much storage space as a float variable because it has greater precision than the float data type. Therefore, it is only recommended to use in situations where the program’s space complexity is less important than the accuracy of the value.
• When a variable is initialized with a double, it has 64 bits of precision for a decimal point value. The 64 bits are broken up into a number of pieces, each with a distinct function. The sign is stored in the first bit, and the exponent number is stored in the following 11 bits. The actual variable of the number is stored in the remaining 52 bits. 15 digits can be stored in a double.
• In comparison, a variable initialized with float has a 32-bit precision for a decimal point number. The exponent number is kept in the next 8 bits. The actual variable of the number is stored in the remaining 23 bits. The accuracy of the float is 7 decimal digits.

### Examples of Float in C

In this section, we will discuss various examples of float in c.

Example 1 of Float in C: Declaring the Variable
Here we will see the code implementation of the above-mentioned example.

Code Implementation

```#include <stdio.h>
int main()
{
float num;
num = 10.000;

printf("The value of the num is %f \n", num);
return 0;
}```

Output

``The value of the num is 10.000000 ``

Explanation of the above example
In the above example we have declared a float type number and then tries to print it. The result will be shown up to six decimal places.

Example 2 of Float in C: Declaring multiple variables in the same line
Here we will declare multiple float variables in the same line. We will see its code implementation.

Code Implementation

```#include <stdio.h>
int main()
{
float num1=8.15, num2=15.5;

printf("The value of num1 is %f \n", num1);
printf("The value of num2 is %f \n", num2);
return 0;
}```

Output

``````The value of num1 is 8.150000
The value of num2 is 15.500000 ``````

Explanation of the above example
In the above example we have declared two floating type numbers in the same line and then printed their corresponding value and the answer is upto 6 decimal places.

### Uses of Float in C

There are many uses of float in c. Some of them are mentioned below:

• It is very helpful in calculations involving decimal numbers like in physics calculations, financial calculations, and some other scientific calculations.
• It is used in computer graphics and animations as they are used to store the coordinates of the objects.
• It is used in input and output operations that have fractional components like temperature and percentage.
• They are widely used in gaming and simulations to represent physical components like velocity, position, acceleration, and rotation.
• They are used in machine learning and data analysis, as they are used to represent numerical values like biases, probabilities, weights, and confidence scores.

Conclusion
In conclusion, the float data type in C is used to store floating-point numbers, which are numbers with a decimal point. Floats have limited precision, but they are useful for many scientific and mathematical applications. To declare a float variable, you use the keyword "float" followed by the variable name. Floats can be used in arithmetic expressions just like any other variable, and you can control the number of decimal places that are displayed when you output a float variable using a format specifier.

Below are some of the frequently asked questions about float in c.

1. What is the range of values that can be stored in a float in C?
The range of values that can be stored in a float in C depends on the size of the float, but typically it is between -3.4 x 10^38 and 3.4 x 10^38.

2. Can a float be negative in C?
Yes, a float can be negative in C.

3. Can a float be used in arithmetic expressions in C?
Yes, a float can be used in arithmetic expressions in C just like any other variable.

4. How is a string converted to a float in C?
A string can be converted to a float in C using the "atof" function.

5. Can a float be used as an index in an array in C?
No, a float cannot be used as an index in an array in C.