- Float is one among the basic data type in C.
- Floating point numbers in C has precision up to 6 decimal places.
- The header file float.h defines macros that allows you to use values and other binary representation of real numbers in the program.
- double data type has precision up to 15 decimal places.
- By default all the decimal numbers in C are double.
- long double data type has precision up to 19 decimal places.
- The following table shows the range of values for each data type,
- Float data type has the highest order of precedence over all the other data types. So, doing arithmetic operation with float and any other data type will results in float only.
- Modulo operator (%) does not work on float.
- If the value of the float variable exceeds the range then roll over happens.
Let us look into some examples for understanding it better,
Examples:
#include<stdio.h>
int main()
{
float a = 4.3;
if (x == 4.3)
printf("Inside if");
else if (a == 4.3f)
printf("Inside else if");
else
printf("Inside else");
}
The output of the above code is 'Inside else if' because by default all the decimal numbers in C are considered as double ans so first if condition will be false. Because comparing two different data types will also be false no matter what their values are.
#include<stdio.h>
int main()
{
float a = 1.5;
if (x == 1.5)
printf("Inside if");
else if (a == 1.5f)
printf("Inside else if");
else
printf("Inside else");
}
In this examples output is 'Inside if' because if we convert 1.5 into its equivalent decimal then we can see that after one place place all the remaining digits will be zero i.e 1.1000... whatever the precision may be so the If condition will be true.
For more examples and quizzes follow our Instagram page @learnclang.
No comments:
Post a Comment