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 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;
Note:
The storage size of float data type is dependent on compiler which is same as int data type.
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
Note:
When a float variable is assigned an integer value , then the result will be a float value with zeroes after the decimal point. It is important to use the correct format specifier, %f, when printing float values as mentioned above, as using the %d specifier will not produce the expected output and may result in a garbage value being displayed as in case of example 2.
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
Login/Signup to comment