float Data Type in C: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-28 12:59:17   18328  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

  • Float data type allows a variable to store decimal values.
  • Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
  • We can use up-to 6 digits after decimal using float data type.
  • For example, 10.456789 can be stored in a variable using float data type.

    Example:

     float f1 = 234.5 

Storage size of float

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places

float Variable Declaration and Variable Initialization:

Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.

 float pi ;  

Variable Initialization : To initialize a variable you must assign it a valid value.

 float = 3.14 ;  

float Variable Declaration and Variable Initialization in two steps:

float pi;
pi = 3.14;

float Datatype in c Programming

Program

 

#include<stdio.h>
  
void main() {
    float pi;
    pi = 3.14;
    printf("%f\n",pi);
 } 

Output

When you compile and execute the above program, it produces the following result

3.140000
Press any key to continue . . .

float Variable Declaration and Variable Initialization in one line:

float pi
 = 3.14;

float Datatype in C Programming

Program

 
 
#include<stdio.h>
  
void main() {
    float pi = 3.14;
    printf("%f\n",pi);
 } 

Output

When you compile and execute the above program, it produces the following result

3.140000
Press any key to continue . . .

Example of float

Program

 

#include<stdio.h>
  
void main() {
// this is declaration and initialization of variable a
	  // datatype is float
	  float a = 234.5 ;
	  // this is declaration and initialization of variable b
	  // datatype is float
      float b = 234.565 ;
      printf("%f \n",a); // it will print a variable
      printf("%f \n",b); // it will print b variable
 } 

Output

When you compile and execute the above program, it produces the following result

234.500000
234.565002
Press any key to continue . . .