Double Specifier in C
What is a double specifier?
The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of the float type is what we typically connect with the name.
In the C programming language, a double data type is used to hold high-precision floating-point data or integers in computer memory. Because it can carry data of a double size compared to the float data type, it is known as the double data type. Eight bytes, or 64 bits, make up a double. A double data type uses 52 bits for the mantissa and the remaining 1 bit for sign representation. The range of double is 1.7E-308 to 1.7E+308. Real numbers (1, 10), decimal numbers (0.1, 11.002), and negative numbers can all be used to represent double data (-1, -0.00002). It can carry roughly 15 to 16 digits both before and after the decimal point.
Declaring variables in C double
Declaration:
A variable can be declared in the same way as any other type:
double price= 2.90;
Specifier:
The d specifier may be added to the value's end as follows:
double length = 3.69d
However, all floating point numbers in C are by default treated as doubles. As a result, even while you can use it to explicitly indicate that this won't be a float value (float values, on the other hand, must be accompanied by the float specifier f: float height = 177.50f), you probably won't see it used very often.
Printing:
The format specifier%lf is used for printing (%lg,%le, or%la are similar). For compilers that adhere to C99 or later, this is the only appropriate method. In any case, a lot of compilers continue to output doubles using the old%f and adhere to the ANSI standard.
printf("price:%lf", "price");
reading:
With all compilers, reading with scanf is accomplished with %lf.
double price;
scanf("%lf", &price);
printf("price: %lf", price);
Double specifier representation in C
The precise precision of a double in C depends on the implementation. Today's common compiler standard is IEEE-754 (even though most compilers do not conform to the 2019 active revision, but instead to the superseded standards from 2008 and 1985). The compilers use 64 bits to represent the numbers in the following ways:
- The symbol is represented by the first bit. 1 denotes a negative, and 0 is positive.
- 52 significand bits (mantissa)
- 11 exponent bits
It can hold up to 16 digits before the variable is filled to the limit. For example, we can maintain the value of pi with an accuracy of 15 digits after the decimal:
double pi = 3.141592653589793;
Information will be lost if we attempt to keep an excessive amount of data in a variable. Right to left, the information will be lost:
double bigNumber = 34326764.97765354476;
printf("big num: %lf\n", bigNumber);
With the last digit rounded up to 6, this will print 34326764.97765354476
for a total of 19 digits.
In practice, double's range and precision are typically more than adequate.
It can be used either %e, %E, %f, %g, or %G for the double specifier
Example programs
Example 1:
#include<stdio.h>
int main()
{
double d = 123.32445;
printf("Value of d = %f\n",d); //using %f format specifier
return 0;
}
Output
Value of d = 123.32445;
Example 2
#include<stdio.h>
int main()
{
double d = 17;
printf("Value of d = %f\n",d); //using %f format specifier
return 0;
}
Output
Value of d = 17.000000
Long double specifier
A long double specifier is a specifier which stores 16 bytes of memory that is 128 bits of memory.
For long double format specifier, it can be represented as %Lf
#include<stdio.h>
Example programs
Example 1:
int main()
{
double d = 123.32445;
//using %lf format specifier
printf("Value of d = %lf\n",d);
return 0;
}
Output
Value of d = 123.324450
Example 2
#include<stdio.h>
int main()
{
double d = 25.972122;
//using %lf format specifier
printf("Value of d = %lf\n",d);
return 0;
}
Output
Value of d = 25.972122
Let us see the example that prints the value of d using both %f and %lf
Example 1
#include<stdio.h>
int main()
{
double d = 9.256335475;
//using %f format specifier
printf("Value of d = %f\n",d);
//using %lf format specifier
printf("Value of d = %lf\n",d);
return 0;
}
Output
Value of d = 9.256335
Value of d = 9.256335
Example 2:
#include<stdio.h>
int main()
{
double d = 2.34;
//using %f format specifier
printf("Value of d = %f\n",d);
//using %lf format specifier
printf("Value of d = %lf\n",d);
return 0;
}
Output
Value of d = 2.340000
Value of d = 2.340000