Question
Correct Format Specifier for double in printf in C
Question
In C, what is the correct format specifier for printing a double value with printf? Should it be %f or %lf?
I think %f is correct, but I am not completely sure.
#include <stdio.h>
int main(void)
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
return 0;
}
Short Answer
By the end of this page, you will understand how printf handles double values in C, when %f and %lf are valid, and why printf and scanf behave differently for floating-point types. You will also see common mistakes, practical examples, and a small project to reinforce the idea.
Concept
In C, the correct and standard format specifier for printing a double with printf is %f.
For printf-style functions:
%fprints a floating-point valuedoublearguments are expected for%f%lfinprintfis typically treated the same as%fby modern compilers and libraries, but the important thing to learn is that%fis the normal specifier fordoubleinprintf
Why this happens
The reason is based on default argument promotions in variadic functions like printf.
When you pass arguments to printf, there is no fixed parameter type information for the extra arguments, so C applies promotions:
floatbecomesdouble
Mental Model
Think of printf as a display clerk who receives all decimal numbers in one standard delivery box: double.
Even if you send a float, it gets repackaged into a double before arriving. So the clerk expects the %f label when printing normal floating-point values.
Now think of scanf as a storage clerk who must place the value into the correct size box in memory:
%fmeans “store into afloat*”%lfmeans “store into adouble*”
So:
printfcares about the promoted value type it receivesscanfcares about the exact pointer type where it will write the value
Syntax and Examples
Core syntax
printf("%f", value);
For a double:
double d = 1.4;
printf("%f", d);
Example: printing a double
#include <stdio.h>
int main(void)
{
double d = 1.4;
printf("%f\n", d);
return 0;
}
Output:
1.400000
By default, %f prints 6 digits after the decimal point.
Example: controlling precision
#include <stdio.h>
int main
{
pi = ;
(, pi);
(, pi);
(, pi);
;
}
Step by Step Execution
Consider this example:
#include <stdio.h>
int main(void)
{
double d = 1.4;
printf("Value = %.2f\n", d);
return 0;
}
Step-by-step
1. A double variable is created
double d = 1.4;
dstores a floating-point number- its type is
double
2. printf is called
printf("Value = %.2f\n", d);
The format string contains:
Value =-> printed as plain text%.2f-> print a floating-point value with 2 digits after the decimal point
Real World Use Cases
Floating-point output appears in many real programs.
Common examples
- Finance tools: printing totals, tax, interest, or exchange rates
- Sensor applications: showing temperature, pressure, or voltage readings
- Scientific programs: printing measurements and computed results
- Game development: displaying coordinates, speed, or frame timing
- APIs and logs: recording decimal values for debugging or monitoring
Example: temperature logger
#include <stdio.h>
int main(void)
{
double temperature = 23.6789;
printf("Temperature: %.1f C\n", temperature);
return 0;
}
Example: price display
#include <stdio.h>
int main(void)
{
double price = 19.99;
printf("Price: $%.2f\n", price);
return 0;
}
In these cases, choosing the right precision is often as important as choosing the right format specifier.
Real Codebase Usage
In real codebases, developers rarely use printf with just bare %f everywhere. They usually combine it with formatting decisions, validation, and readability patterns.
Common patterns
Fixed precision for user-facing output
printf("Average response time: %.2f ms\n", avg);
This avoids noisy output like many unnecessary decimal places.
Debug logging
printf("x=%f, y=%f, z=%f\n", x, y, z);
Useful in small tools or command-line programs.
Guarding calculations before printing
if (count == 0)
{
printf("No data available.\n");
return 0;
}
printf("Average: %.2f\n", total / count);
This is a simple guard clause pattern: check invalid conditions early.
Input/output pairing
Developers often read a double with scanf("%lf", &value) and later print it with .
Common Mistakes
1. Mixing up printf and scanf
This is the most common mistake.
Wrong idea
%lfmust always be used fordouble
That is not the rule.
Correct rule
printfuses%ffordoublescanfuses%lffordouble
2. Using %d for a floating-point value
Broken code:
double d = 1.4;
printf("%d\n", d);
Why it is wrong:
%dis forintdis a
Comparisons
printf vs scanf for floating-point values
| Function | float | double | Why |
|---|---|---|---|
printf | %f | %f | float is promoted to double |
scanf | %f | %lf | scanf needs the exact pointer type |
%f vs in
Cheat Sheet
Quick rules
- Use
%fto print adoublewithprintf floatalso uses%finprintf- Use
%lfto read adoublewithscanf - Use
%fto read afloatwithscanf
Common syntax
printf("%f", d);
printf("%.2f", d);
printf("%e", d);
printf("%g", d);
Examples
double d = 1.4;
printf("%f\n", d); // 1.400000
printf("%.1f\n", d);
(, d);
FAQ
Is %lf wrong in printf?
In practice, %lf is treated the same as %f in printf on standard C implementations. But %f is the usual and clearer choice for printing a double.
Why does scanf use %lf for double but printf does not?
Because printf receives promoted arguments, while scanf writes into memory through pointers and must know the exact destination type.
Can I print a float using %f in printf?
Yes. A float is promoted to double when passed to printf, so %f is correct.
What does print by default?
Mini Project
Description
Build a small console program that reads a few decimal values, calculates their average, and prints the result with different printf floating-point formats. This reinforces the difference between reading with scanf and printing with printf, while giving practice with precision control.
Goal
Create a C program that reads three double values, computes the average, and displays the result using %f, %.2f, and %g.
Requirements
- Read three decimal numbers from the user as
doublevalues. - Validate that each input was read successfully.
- Compute the average of the three values.
- Print the average using fixed-point output and custom precision.
- Also print the average using
%gfor compact output.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C printf Format Specifier for bool: How to Print Boolean Values
Learn how to print bool values in C with printf, why no %b/%B specifier exists, and the common patterns to print true/false or 0/1.
Calling C or C++ from Python: Building Python Bindings
Learn the quickest ways to call C or C++ from Python, including ctypes, C extensions, Cython, and binding tools with practical examples.