×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Correct format specifier for double in printf() in C

Learn about the double type in C, and its format specifier with printf() and scanf().
Submitted by Shubh Pachori, on July 10, 2022

A double is a datatype in C programming language that stores high-precision floating-point data or numbers in the computer memory location. It is known as a double datatype because it can store the double size of data as compared to the float datatypes. The double datatypes are of 8 bytes and its range is -1.7E-308 to 1.7E+308. It can store up to 15 to 16 digits before and after the decimal point. Format specifiers for a double datatype are different from a float, when we use it with the scanf() function but in printf() function we can use %f for both float and double datatypes.

Format specifier for double in printf() in C

When we use the printf() function for the floating-point data we can use %f for both float and double, and %Lf for long double, and we can use %lf also for double. In the printf(), there is no difference but when we use scanf() function for the floating-point data we have to use %f for float, %lf for double, and %Lf for long double.

Example of Format specifier for double in printf() in C

C language code for better understanding using various format specifiers for double datatypes

#include <stdio.h>

int main()
{
    float f1, f2; //declaring two different float variables
    double d1, d2; //declaring two different double variables
    long double ld1, ld2; //declaring two different long double variables

    //case 1 when we use %f for both double and float in printf()
    printf("Enter float Value: ");
    scanf("%f", &f1);
    printf("Enter double Value: ");
    scanf("%lf", &d1);
    printf("Enter long double Value: ");
    scanf("%Lf", &ld1);

    //here we use %f for float and double both
    printf("\nfloat: %f\ndouble: %f\nlong double: %Lf\n", f1, d1, ld1); 

    printf("\n");

    //case 2 when we use %f for float and %lf for double in printf()
    printf("Enter float Value: ");
    scanf("%f", &f2);
    printf("Enter double Value: ");
    scanf("%lf", &d2);
    printf("Enter long double Value: ");
    scanf("%Lf", &ld2);

    //here we use %f for float and %lf for double
    printf("\nfloat: %f\ndouble: %lf\nlong double: %Lf\n", f2, d2, ld2); 
    
    return 0;
}

Output:

Enter float Value: 123.321
Enter double Value: 123456.654321
Enter long double Value: 1234567890.987654321

float: 123.320999
double: 123456.654321
long double: 1234567890.987654

Enter float Value: 123.456
Enter double Value: 456789.9876
Enter long double Value: 987654321.123456 

float: 123.456001
double: 456789.987600
long double: 987654321.123456

In the above output, we can see that there is no difference in the output of %f and %lf format specifier for double datatype in printf() function. Hence, we can use both %f and %lf in printf() function without any problem.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.