For instance, 4.5672, 2.45354, -5.22234, 3.12345678901, 0.15197e-7, and so on.
Declaration and Initialization of double variable
Declaring a double variable in C involves specifying the data type as "double" along with assigning a unique variable name.
double data1;
Assigning a value to a double variable during initialization involves specifying a legitimate value for the variable name.
Data1 = 3.2325467;
Moreover, it is possible to both declare and set the variable name in one line of code.
double data1 = 3.2325467;
Program to get the size of data types using sizeof function
Let's explore an illustration demonstrating how to display the size of each data type in the C programming language.
Get_size.c
#include <stdio.h>
#include <conio.h>
void main()
{
// use sizeof() function to get the size of data type in c
printf (" The size of int data type is %d", sizeof(int));
printf (" The size of char data type is %d", sizeof(char));
printf (" The size of float data type is %f", sizeof(float));
printf (" The size of double data type is %f", sizeof(double));
getch()
}
Output:
The size of int data type is 4
The size of char data type is 1
The size of float data type is 4
The size of double data type is 8
In the program mentioned above, the sizeof function is employed to determine the size of various data types such as integer, float, character, and double by passing int, char, float, and double as arguments.
Program to convert feet into meter using the double data type
Let's take an instance where we pass a pair of numerical values as arguments to a function, followed by converting the measurement from feet to meters.
Prog.c
#include <stdio.h>
double feet_to_meter (double f); // declare a user defined function
int main()
{
double feet, cnvt; // declare a variable as the double data type
printf (" Enter the feet in double ");
scanf("%lf", &feet);
cnvt = feet_to_meter(feet); // call feet_to_meter function
printf (" Converted feet to meter is: %lf", cnvt);
return 0;
}
// definition of the function
double feet_to_meter (double f)
{
return f / 3.28;
}
Output:
Enter the feet in double 45.78
Converted feet to meter is: 13.957317
Program to convert an integer data into double data type
Let's explore an illustration of converting an integer value to a floating-point number in the C programming language.
Prog2.c
#include <stdio.h>
int main()
{
int sum = 17, count = 5;
double var;
var = (double) sum / count;
printf (" Convert an int value into double data type is: %lf \n", var);
}
Output:
Convert an int value into double data type is: 3.400000
Program to convert Celsius to Fahrenheit temperature
Let's explore a program that transforms a specified Celsius temperature into Fahrenheit using C programming language.
Convert.c
#include <stdio.h>
int main()
{
// declaration of double variable
double c_temp, f_temp;
printf(" Enter the temperature in Celsius: ");
scanf (" %lf", &c_temp); // accept the Celsius temperature
f_temp = ( c_temp * 1.8) + 32; // use conversion formula
printf (" The temperature in Fahrenheit is: %lf", f_temp);
return 0;
}
Output:
Enter the temperature in Celsius: 56.8
The temperature in Fahrenheit is: 134.240000
Program to print the sum of two double number using function
Let's explore a program in C that calculates the sum of two double precision floating-point numbers by utilizing a function.
double.c
#include <stdio.h>
double sum_num(double p, double q);
int main()
{
// declaration of the double variables
double x, y, res;
printf (" Enter two double numbers ");
scanf(" %lf %lf", &x, &y); // take two double variable from user
res = sum_num(x, y); // call double function
printf (" The result of two double number is: %lf", res);
return 0;
}
double sum_num(double p, double q)
{
return p + q; // return the sum of double values
}
Output:
Enter two double numbers 34.798
43.567
The result of two double number is: 78.365000
float vs. double
The float data type represents a single-precision value in programming, storing a 32-bit floating-point number or decimal value within 4 bytes of memory. It is a fixed data type with a set name and characteristics that cannot be altered. In comparison to the double data type, float is known for its faster processing speed due to its narrower range. Typically, a float variable can accommodate around 7 significant digits. The permissible range of values for the float data type spans from 1.5 x 10 -45 to 3.4 x 10 38.
A double data type is a data type that can store 64 bits of information for floating-point or decimal numbers, equivalent to 8 bytes. This data type is predefined and its name and purpose cannot be altered. Due to its larger size, it is relatively slower than the float data type. A double can hold a range of 15 to 17 digits and can accommodate values from 5.0 x 10^-345 to 1.7 x 10^308.
Conclusion:
A double data type represents double-precision floating-point data, with the ability to store data twice the size of float data type. This is achieved by utilizing 64 bits of computer memory, as opposed to the 32 bits used by the float data type. The double data type is commonly employed in scenarios requiring extensive number computation, where precision in mantissa plays a crucial role. Programmers often opt for the double data type due to its capability to deliver precise results for decimal calculations involving complex numbers.