Float in C

Float is a data type that enables the user to declare variables and assign floating point values to the variable.

The syntax for declaring float variable

The data type is used to declare the numbers with decimal points. Its syntax is as follows:

You can also use the float keyword once to declare multiple floating point variables at once.

Parameters or Arguments

  • var1: It states the name of the first variable declared by the user.
  • val1: It is not necessary to assign the value to the variable while declaring it. This parameter is optional to use. The user can also take user input to assign value to the variable.
  • varn: It is the name for the nth variable.
  • valn: It is optional. It will assign the value to the nth variable.

For Example:

Now let us see examples to learn how to use the float in your C program.

Example 1: Declare the Variable

In the code below, we have used the float keyword to declare the roi variable in the program.

Output:

Float in C

Example 2: Declare the Variable and assign the value to the variable

In the code below, we have declared the variable and assigned the value to the variable in a single statement. This reduces unnecessary lines of code.

Output:

Float in C

Example 3: Declare Multiple Floating Point Variable in a single line

The user can also declare multiple variables in a single line using the datatype only once. To declare multiple variables in a single statement, the user must separate the variable names by commas (,).

Let us see an example to implement the statement in your code.

Output:

Float in C

Example 4: Declaring multiple floating point variables and assigning the values to the variable in the same statement.

It is possible to declare different variables simultaneously until they have the same datatype. To declare floating point variables and assign the values to the variable, use the float keyword, followed by key=value pairs of the variable name and value, each separated by a comma (,).

Output:

Float in C

Differentiate between float and double datatype in C Programming

There are two data types in C and C++ that allow users to take and return decimal point values in the system. These data types are float and double, and the user can use both these data types to represent such numbers.

Now the question arises why use two different data types for representing the same type of data and what is the difference between these two data types?

  • The major difference between float and double is based on their precision. The precision determines the degree of accuracy of the variable value. The precision for the double time is 2 times more than the precision of the float data type. In simpler terms, it means that the variable that is initialized uses double, double precision rather than the variables initialized using the float data type.
  • The number of precision of a variable that is initialized using double is 64-bit precision for a decimal point number. The 62 bits are divided into several parts, each with its own role. The first bit is used for storing the sign, and the next 11 bits are used for storing the exponent value. The remaining 52 bits are used for storing the actual variable of the value. The double can store 15 decimals.
  • In contrast, the precision of a variable that is initialized using float is 32-bit for a decimal point number. The next 8 bits are used for storing the exponent value. The rest of the 23 bits are used for storing the actual variable of the value. The float can have 7 decimal digits of precision.
  • The precision of a double data type is more than the float; therefore, it requires double the amount of space required to store a float variable. So, it is only preferred to use where the accuracy of the value holds more importance than the space complexity of the program.

Next Topicislower() in C