Float Data Type in C

C-Float Data Type

On this page we will discuss about Float Data Type which is used in C.
The Float data type is used to store numbers with decimal points of single precision.(upto 6-7 digits).
float data type in C

Float Data Type used in C

In C programming , the float data type is used to store both decimal and exponential values and is represented by %f format specifier.

The float data types with their storage size, range and precision are given below in the table:

Type Storage Size (bytes) Range Precision
float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places
long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal places

Declaration of Float type variable

float variable_name;

Initialization of Float type variable

A float variable can be initialized in two ways as follows:

1. By assigning value to a  variable using assignment operator.

price = 34.511e2;
2. By assigning value to a variable during declaration only using assignment operator.
float price = 34.511e2;

Program to Demonstrate the use of Float Data Type

Example 1:

Run
#include <stdio.h>
 
int main()
{
    // Initialization of temporary float variables.
    float x = 13.0f;
    float y = 4.6f;
    float z = 5E-6f;
    
    // Display the value of variables.
    printf("Value of variable x = %f\n",x);
    printf("Value of variable y = %f\n",y);
    printf("Value of variable z = %f",z);

  return 0;
}

Output:

Value of variable x = 13.000000
Value of variable y = 4.600000
Value of variable z = 0.000005

Example 2:

Run
#include <stdio.h>

int main() {
	
	// Declaring temporary variables
	float sum_of_marks=8775.33;
	float number=89;
	float average_of_marks=(sum_of_marks/number);
	
	// Display result of calculation
	printf("Average of marks is %f \n", average_of_marks);
	printf("Value of number is %f \n",number);
	printf("Value of number presented as an integer %d \n",number);
}

Output:

Average of marks is 98.599213 
Value of number is 89.000000 
Value of number presented as an integer -1346559328 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription