If you're someone who's just beginning to learn C programming, you might have already come across printf() and scanf(). These two functions help you interact with the user by printing messages and taking input.
But there’s a small thing hidden inside them that plays a big role: format specifiers.

They may seem like a confusing term at first, but once you understand what they are and why they’re used, you’ll find them quite easy.
In this article, we’ll talk about what a format specifier is, especially for the double data type. We’ll go over how it works, how to use it, and also clear up a few common mistakes beginners make. The goal is to make this as easy and friendly as possible-like you’re learning from a friend
Let’s break it down simply. In C programming, a format specifier is just a symbol you write inside printf() or scanf() to tell the computer what type of data to expect. It could be an integer, a decimal number, a character, or even a string.
Let’s take a basic example:
Copy Code
int age = 21;
printf("My age is %d", age);Here, %d is the format specifier. It means that the value we’re printing is an integer.
Similarly, if you’re working with decimal numbers (like 3.14), you need a different format specifier that matches that type of value.
This is where the double data type and its format specifier come in.
In simple terms, a double is a type of variable in C used to store decimal values. It’s similar to float, but more accurate and capable of holding more digits after the decimal point.
Imagine you're working on a project where you need to store the value of pi accurately. You could write:
double pi = 3.1415926535;
Now, if you had used a float instead, it might have rounded off after a few digits. But double keeps it more precise. That’s why double is preferred when you need accuracy-for example, in scientific calculations, bank transactions, fuel consumption, or currency conversion.
When you want to use double in your program and print or read it using printf() or scanf(), you need to use the correct format specifier. For double, the correct one is:
%lf
Here’s how it works:
Copy Code
double value = 9.8765;
printf("The number is %lf", value);
This will print:
The number is 9.876500Notice something? It prints six digits after the decimal by default. That’s just how C handles it unless you tell it otherwise.
This is a question many learners ask. The answer depends on whether you’re using printf() or scanf():
In printf(), both %f and %lf can be used. When you pass a float, it gets automatically promoted to double.
But in scanf(), you must use %lf for double. If you use %f by mistake, the input won’t be stored properly.
So, to stay consistent and avoid confusion, it’s always safe to just use %lf with double.
Here are two simple examples to help you understand better.
Example 1: Printing a Double
Copy Code
#include <stdio.h>
int main() {
double weight = 65.4321;
printf("Weight: %lf kg\n", weight);
return 0;
}
Output:
Weight: 65.432100 kgExample 2: Taking Input From User
Copy Code
#include <stdio.h>
int main() {
double height;
printf("Enter your height: ");
scanf("%lf", &height);
printf("You are %lf meters tall\n", height);
return 0;
}You can try these programs yourself and see how they behave.
Sometimes, you don’t want to show six decimal digits. You might want to show only two, especially if you’re dealing with prices, distances, or anything that should look clean and easy to read.
In such cases, you can write:
Copy Code
printf("%.2lf", value);Here’s an example:
Copy Code
double price = 1234.56789;
printf("Price: %.2lf\n", price);
Output:
Price: 1234.57It automatically rounds it off and shows only two digits after the decimal.
Let’s look at some frequent mistakes people make when they’re starting out:
It won’t work correctly. Always use %lf when using scanf() with a double.
Without it, the program won’t know where to store the input.
If you want your numbers to look clean (like two decimal places), always specify it using %.2lf or similar.
Use the right specifier for the right data type. It’s a small detail, but it matters a lot.
You might be wondering-where do I actually use all this? Let’s say you’re building a weather app, and you want to show the temperature:
Copy Code
double temperature = 37.5863;
printf("Current Temperature: %.1lf°C\n", temperature);Or you’re working on a fuel tracking system:
Copy Code
double distance = 120.5;
double fuel = 7.2;
double mileage = distance / fuel;
printf("Mileage: %.2lf km/l\n", mileage);Or you’re calculating interest in a banking app:
Copy Code
double principal = 5000.00;
double rate = 4.5;
double time = 2;
double interest = (principal * rate * time) / 100;
printf("Interest: %.2lf\n", interest);These are just basic examples. There are countless real-world scenarios where you’ll need double and its format specifier.
Let’s go over everything one more time:
double is used for decimal values where precision is important.
The correct format specifier for double is %lf.
Use %lf in both scanf() and printf(), even though %f also works in printf().
Use .2, .3, etc. inside the format to control •how many digits you want to show after the decimal.
Avoid using %f in scanf() when reading a double.
Always use & when taking input using scanf().
If you’re serious about learning C programming, check out the beginner-friendly courses available on Uncodemy. They offer practical lessons that help you build real projects while mastering the basics.
Whether you prefer watching videos or reading blog articles like this one, they’ve got you covered with easy-to-understand content. You’ll find hands-on examples, coding challenges, and even interview tips to help you grow as a developer.
Check out https://uncodemy.com/blog/ for more helpful articles.
Let’s be honest-when you're starting out in C programming, things like format specifiers can feel like small details you just want to skip over. But the truth is, these “small” things actually make a huge difference in how your code works and how confident you feel while writing it. And the %lf format specifier, especially for double, is one of those tiny bits of knowledge that unlocks so much more.
If you've made it this far in the blog, that already shows you're serious about understanding the basics-not just memorizing things, but truly getting how they work. That mindset? That’s what sets apart someone who’s just learning code from someone who becomes a real developer. So give yourself credit. You’re doing better than you think.
At first, using %lf or knowing when to add a .2 to limit decimal places might feel like one more rule to remember. But as you practice, it starts to come naturally. You’ll begin writing a program and instinctively know, “Okay, this needs to show just two digits after the point,” or “I should use %lf because this is a double.” You stop second-guessing and start solving problems faster. And that’s the best feeling ever.
Also, don’t worry if you mess up along the way. Everyone forgets the & in scanf() at least once. Everyone accidentally uses %f and gets weird input results. It’s part of the learning curve. Mistakes like these aren’t failures-they’re lessons. Every little bug you solve sharpens your skills more than any theory ever could.
Another thing to keep in mind is: you don’t have to be perfect. So many beginners think they need to know everything before writing a “real” program. That’s not true at all. You grow by building. Even the smallest project-like a BMI calculator or fuel tracker-can teach you how to use double, how to take input correctly, how to manage output formatting. These are not just exercises. They are your training ground.
And lastly, be patient with yourself. Coding is not a race. It's more like learning a new language-you won’t be fluent on Day 1. But with every line of code you write, every example you try, you’re building fluency. You’re training your brain to think logically, spot patterns, and communicate with the computer in its own language. That’s powerful.
So, don’t let technical terms scare you. You now know what a format specifier is, what %lf does, why it matters, and how to use it correctly. That’s more than enough to take your next step forward.
Keep practicing. Keep experimenting. Keep believing in your progress.
Because you’re not just learning to code-you’re learning to think like a developer. And that journey? That’s the most exciting part. 🌱💻
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR