top

Search

C Tutorial

.

UpGrad

C Tutorial

Double In C

Introduction

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!

What is Double in C?

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. 

Syntax for Declaration

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>
int main() {
    double length;
    double width;
    double area;
    length = 4.5;
    width = 3.2;
    area = length * width;
    printf("The area of the rectangle is %.2f\n", area);
    return 0;
}

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

Initialization of Double Variable

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>
int main() {
    double pi = 3.14159;
    double gravity = 9.8;
    printf("The value of pi is %.5f\n", pi);
    printf("The value of gravity is %.2f\n", gravity);
    
    return 0;
}

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
The value of gravity is 9.80

How to Print the Double Value in C?

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;
printf("The value is %f\n", variable_name);

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. 

Representation of Double in C

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,

  • Sign Bit - 1 byte represents the sign of the number, wherein 0 denotes positive, and one denotes negative. 

  • Exponent - 11 bits that represent the exponent of the number. It determines the scale of the number and allows the representation of a wider range of values. 

  • Significand - 52 bits that represent the significant digits of the number. It is responsible for storing the accuracy and precision of the value. 

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. 

Some Programs of Double Data Type in C

Mentioned below are a few examples of programs that involve the use of double data type in C.

1. Program to Get the Size of Data Types Using sizeof() Function

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>
int main(void) {
    const int SIZE_OF_CHAR = sizeof(char);
    const int SIZE_OF_INT = sizeof(int);
    const int SIZE_OF_FLOAT = sizeof(float);
    const int SIZE_OF_DOUBLE = sizeof(double);
    const int SIZE_OF_LONG = sizeof(long);
    const int SIZE_OF_LONG_LONG = sizeof(long long);

    printf("Size of character: %d bytes\n", SIZE_OF_CHAR);
    printf("Size of integer: %d bytes\n", SIZE_OF_INT);
    printf("Size of floating-point number: %d bytes\n", SIZE_OF_FLOAT);
    printf("Size of double: %d bytes\n", SIZE_OF_DOUBLE);
    printf("Size of long: %d bytes\n", SIZE_OF_LONG);
    printf("Size of long long: %d bytes\n", SIZE_OF_LONG_LONG);
    return 0;
}

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
Size of integer: 4 bytes
Size of floating-point number: 4 bytes
Size of double: 8 bytes
Size of long: 8 bytes
Size of long long: 8 bytes

2. Program to Convert Feet into Meters Using the Double Data Type

Moving on, here is a simple program that converts feet into meters using the double data type in C.

#include <stdio.h>
int main() {
    double feet, meters;
    printf("Enter the length in feet: ");
    scanf("%lf", &feet);
    // Convert feet to meters
    meters = feet * 0.3048;
    printf("Length in meters: %.2f\n", meters);
    return 0;
}

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
Length in meters: 3.05

3. Program to Convert an Integer Data into Double Data Type

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>
int main() {
    int integerData = 10;
    double doubleData;
    doubleData = (double)integerData;
    printf("Integer Data: %d\n", integerData);
    printf("Double Data: %.2f\n", doubleData);
    return 0;
}

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
Double Data: 10.00

4. Program to Convert Celsius to Fahrenheit Temperature

#include <stdio.h>
int main() {
    double celsius, fahrenheit;
    printf("Input the temperature in Celsius: ");
    scanf("%lf", &celsius);
    // Convert Celsius to Fahrenheit
    fahrenheit = (celsius * 9 / 5) + 32;
    printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
    return 0;
}

Here, we have declared two variables,

  • ‘celsius’- to store the temperature in Celsius, and

  • ‘fahrenheit’ - to store the converted temperature in Fahrenheit.

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
Temperature in Fahrenheit: 77.00

5. Program to Print the Sum of Two Double Numbers Using Function

#include <stdio.h>
double calculateSum(double num1, double num2) {
    return num1 + num2;
}
int main() {
    double number1, number2, sum;
    printf("Enter the first number: ");
    scanf("%lf", &number1);
    printf("Enter the second number: ");
    scanf("%lf", &number2);
    sum = calculateSum(number1, number2);
    printf("Sum: %.2f\n", sum);
    return 0;
}

Output

Enter the first number: 3.14
Enter the second number: 2.5
Sum: 5.64

Float vs Double Data Type in C

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. 

Conclusion

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. 

FAQs

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. 

Leave a Reply

Your email address will not be published. Required fields are marked *