Tutorial Playlist
What is double in C? What purpose does it serve? These are some of the burning questions you are likely to encounter regarding double data type in C. The article below provides the necessary answers to all these questions and shares a few double in C examples to help you understand better.
So without any further ado, let’s get started!
In C programming language, the ‘double’ refers to a data type in C that can be used to store real numbers with a large range of values. Unlike other data types in C, such as floating data type or integers, the double data type stores these large range numbers with higher precision.
The double data type in C occupies as much as 8 bytes of memory and can be allowed for a range of approximately ±1.7 × 10^308. This makes it suitable to be used in scenarios which demand a high range of accuracy, such as financial computations or engineering simulations.
An important thing to remember is that the double data type in C can hold up to 15 to 16 digits before or after the decimal point. For example, 1.9976, 21.987654, etc.
The syntax for declaring a double data type in C includes,
double variable_name;
Let’s explore a small example of declaring double in C.
#include <stdio.h> |
Here we have three double variables, namely, ‘length’, ‘width’ and ‘area’. After assigning values to ‘length’ and ‘width’, we calculate the ‘area’ by multiplying length and width.
The result generated will then be printed using ‘printf’.
Output
The area of the rectangle is 14.40 |
In order to initialise a double variable in C, we first need to specify a valid value for the variable name. It can be a constant value or even a value of a compatible type. The syntax for the same includes,
double variable_name = initial_value;
Let’s take a closer look at a small example of performing the initialization of a double variable in C.
#include <stdio.h> |
Here, we have declared and initialized two variables, namely, ‘pi’ and ‘gravity’. Each of these variables is assigned values of 3.14159 and 9.8, respectively. We have then used ‘printf’ to print the output of these variables.
Output
The value of pi is 3.14159 |
In order to print the value of a double variable in C, we typically use the ‘%f’ format specifier in the ‘printf’ function. The syntax for the same goes as follows,
double variable_name = 3.14; |
Please note that the ‘%f’ format specifier, by default, prints the double value with six decimal places. If you wish to customise the number of decimal places, you can achieve the same by modifying the format specifier. So, for example, if you wish to print with two decimal places, then you can use the ‘%.2f’ format specifier.
The floating point format represents a double in C. It follows the IEEE 754 standard, wherein the bit layout and encoding of the value are specified. The numbers are represented using the 64-bit value, which is also commonly known as the double-precision floating point number. It consists of three parts, namely,
Please note that the exact representation or implementation of double values may vary according to the compiler or the system architecture. Different compilers usually use different representations or varied bit sizes for the sign, exponent and significand.
Mentioned below are a few examples of programs that involve the use of double data type in C.
To accurately determine the size of different data types in C, we generally use the sizeof() function. A small program involving the same goes as follows,
#include <stdio.h> |
The given code calculates and displays the sizes of various data types in C. It uses the sizeof() function to determine the sizes and assigns them to constants. These constants are then printed using printf() statements, providing the sizes in bytes. The code helps in understanding the sizes of different data types in C programming.
Output
Size of character: 1 bytes |
Moving on, here is a simple program that converts feet into meters using the double data type in C.
#include <stdio.h> |
Here, we have two double variables, namely, ‘feet’ and ‘meters’. The user is prompted to enter the length in feet using the printf() function. Following this, the input is read using the scanf() with the ‘%lf” format specifier.
In order to convert the length into meters, it is multiplied by the conversion factor, ‘0.3048’, and the result is stored in the ‘meter’ variable.’ Finally, we use the ‘printf’ function to display the length in meters with two decimal places.
Output
Enter the length in feet: 10 |
In order to convert an integer data into a double data type in C, you simply have to assign the integer value to a double variable.
#include <stdio.h> |
Here, the integer variable, ‘integerData’, is initialised with the value 10. We have then declared a double variable, ‘doubleData’, to store the converted value. Following this, to convert the integer data into a double data type, we make use of a typecast, ‘double’, before the integer variable. It instructs the compiler to treat the integer value as a double value. Finally, the ‘%.2f’ is used to display the double value with two decimal places.
Output
Integer Data: 10 |
#include <stdio.h> |
Here, we have declared two variables,
The user is then prompted to enter the temperature in Celsius using ‘printf’ and read the input using ‘scanf’ with the ‘%lf’ format specifier. After this, we applied the conversion formula (celsius * 9 / 5) + 32 to convert the temperature from Celsius to Fahrenheit. The result generated is stored in the ‘fahrenheit’ variable. Ultimately, we print the result using the ‘printf’ function
Output
Enter the temperature in Celsius: 25 |
#include <stdio.h> |
Output
Enter the first number: 3.14 |
In the C programming language, ‘double’ and ‘float’ are typically used to represent floating-point numbers. Nonetheless, there are a few differences when it comes to the storage size and precision of both these data types in C. Let us explore double vs float in C differences.
Float | Double |
The ‘float’ data type in C occupies 4 bytes (32 bits) of memory | Double data type in C occupies 8 bytes (64 bits) of memory. |
It offers a precision of approximately 6 decimal places. | It offers a higher precision of approximately 15 decimal places. This makes it suitable to be used in scenarios that demand more accurate and precise calculations. |
Calculations using ‘float’ tend to be faster since the CPU’s floating point unit may be optimised for float operations. | Calculations using double tend to be much slower in comparison to float. |
It is typically used in graphic libraries due to its small range | It is generally used for calculations to eliminate any errors related to decimal values. |
Hopefully, with this, you have understood what is double data type in C, and its various functionalities. To sum it all up, double data type in C is essential for applications that require high precision, a wide range of values and compatibility with floating point representation. It provides the required accuracy for scientific calculations, financial applications, and any other situation where a precise numerical calculation is a necessity. If you wish to learn more about the same, do not forget to check out upGrad’s MS in Computer Science course offered under the comprehensive guidance of Liverpool John Moores University.
1. What do we mean by %d for double in C?
The ‘%d’ format specifier is used for printing and formatting integer values using the ‘printf’ function. However, unlike the ‘%f’ format specifier, it is not suitable for a double data type in C. This means that if you use the ‘%d’ format specifier with a double variable, it will result in undefined behaviour.
2. What do we mean by float in C?
Simply put, float refers to a data type in C that is used to represent floating point numbers. It is typically represented using 32 bits of memory and follows the IEEE754 standard for floating point representation.
3. Why is a double data type used in C?
We mostly use the double data type in C for calculations and to eliminate any form of errors related to decimal values. Unlike other data types, such as float, double offers greater precision as it can represent decimal numbers with more significant digits and offer a higher level of accuracy.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...