×

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

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

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

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Flow chart of While loop in C

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

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

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.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

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

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.