×

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:

  1. The symbol is represented by the first bit. 1 denotes a negative, and 0 is positive.
  2. 52 significand bits (mantissa)
  3. 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

Related Topics

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.