×

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

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 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.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.