Float In C

The data type is employed to specify numerical values with fractional parts. The format for declaring this type is as shown below:

Example

float variable_name= value;

You have the option to employ the float keyword singularly to define several floating-point variables simultaneously.

Example

float var1=value, var2=val, var3=val,….varn=valn;

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:

Example

float rateofinterest=10.00;
float roi;

Now, let's explore some examples to understand the usage of the float data type in your C program.

Example 1: Declare the Variable

In the code provided, we utilized the float keyword to define the roi variable within the program.

Example

#include <stdio.h>
int main()
{
   float roi;
   roi = 10.00;
   //it will print 6 digit decimal number after the decimal points.
   printf("The rate of interest for the investment is %f \n", roi);
   return 0;
}

Output:

Output

[Program Output]

Declare the variable and initialize it with a specific value.

In the code snippet provided, we have initialized the variable by assigning a value to it within the same line of code, effectively minimizing the need for additional code lines.

Example

#include <stdio.h>
int main()
{  // we have used a single line to
    //i) declare the variable
    //ii) assign the value to the declared variable
   float secondinterest=12.00;

   //it will print 6 digit decimal number after the decimal points.
   printf("The rate of interest for the second investment is %f \n", secondinterest);
   return 0;
}

Output:

Output

[Program Output]

Declare Several Floating Point Variables in One Line

Users have the option to define several variables in one line by specifying the data type only once. When declaring multiple variables within a single instruction, it is necessary to differentiate the variable names with commas (,).

Let's examine a demonstration to execute the instruction within your code.

Example

#include <stdio.h>
int main()
{  // we have used a single line to
    //declare multiple variables
   float firstinterest, secondinterest;
   firstinterest=9.00;
   secondinterest=13.00;

   //it will print 6 digit decimal number after the decimal points.
      printf("The rate of interest for the first investment is %f \n", firstinterest);
   printf("The rate of interest for the second investment is %f \n", secondinterest);
   return 0;
}

Output:

Output

[Program Output]

Declaring several floating-point variables and initializing them with values in one line of code.

Variables can be initialized at the same time as long as they share a common data type. In order to declare floating-point variables and assign values to them, the float keyword should be utilized. This should be followed by pairs of variable names and values, separated by commas (,).

Example

#include <stdio.h>
int main()
{  // we have used a single line to
    //i) declare multiple variables
    //ii)assign different values to the multiple variables
   float firstinterest=8.15, secondinterest=15.5;

   //it will print 6 digit decimal number after the decimal points.
   printf("The rate of interest for the first investment is %f \n", firstinterest);
   printf("The rate of interest for the second investment is %f \n", secondinterest);
   return 0;
}

Output:

Output

[Program Output]

Differentiate between float and double datatype in C Programming

There are two numeric data types in C and C++ designed for handling decimal values within the system. These data types encompass float and double, offering users the ability to work with and return decimal point values using either of them.

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.

Input Required

This code uses input(). Please provide values below: