What is double in C?
In this topic, we will discuss the double data type in the C programming language. The C language provides four main data types such as int, char, float, and float. The int data type is used to store an integer, the char data type is used to store character type, and the float data type is used to store decimal type data with single precision.
A double is a data type in C language that stores high-precision floating-point data or numbers in computer memory. It is called double data type because it can hold the double size of data compared to the float data type. A double has 8 bytes, which is equal to 64 bits in size. In double data type, the 1 bit for sign representation, 11 bits for the exponent and the remaining 52 bits used for the mantissa. The range of double is 1.7E-308 to 1.7E+308. Double data can be represents in real number (1, 10), decimals (0.1, 11.002) and minus (-1, -0.00002). It can hold approximately 15 to 16 digits before and after the decimal point.
For example, 4.5672, 2.45354, -5.22234, 3.12345678901, 0.15197e-7 etc.
Declaration and Initialization of double variable
Declaration of double variable: To declare a double variable in C, we need to specify the data type and a variable name.
Initialization of double variable: To initialize a variable, we can specify a valid value to a variable name.
Furthermore, we can declare and initialize the variable name in a single line.
Program to get the size of data types using sizeof() function
Let’s consider an example to print the size of all data types in C programming language.
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 above program, we use a sizeof() function to get the size of an integer, float, character, and double data types bypassing the int, char, float, and double as the parameter.
Program to convert feet into meter using the double data type
Let’s consider an example to pass the double data number as the parameter to a function and then convert the feet into meters.
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 consider an example to convert an int number into the double data type number in C.
Convert an int value into double data type is: 3.400000
Program to convert Celsius to Fahrenheit temperature
Let’s consider a program to convert a given Celsius temperature to Fahrenheit in C.
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 consider a program to get the sum of two double numbers using a function in C.
Enter two double numbers 34.798 43.567 The result of two double number is: 78.365000
float vs. double
float data type: The float is a single-precision data type that holds a 32-bit floating-point or a decimal number and equals 4 bytes. It is a predefined data type or keyword whose meaning and name cannot be changed. The float data type is faster than double because its range is small. It can hold approximately 7 digits. Furthermore, the range of float data type is 1.5 x 10-45 to 3.4 x 1038.
double data type: A double is a precision data type hold 64 bits floating point or a decimal number and equals 8 bytes. It is also a predefined data type whose meaning and name cannot be changed. It is slower as compared to the float data type because its size is big. It can holds accommodate 15 to 17 digits. And the range of double data is 5.0 x 10-345 to 1.7 x 10308.
A double data type is double-precision floating pointing data. It can hold the double size of the data compared to the float data type because double takes 64 bits in computer memory, which is larger than 32 bits of float data type. It is usually used to deal with the huge computation of numbers and mantissa for precision makes. Many programmers choose the double data type because it gives accurate decimal-related results for complex numbers.