double and float

Can I Use %f for double in C?

Yes, for printing: printf uses %f for a double. Reading with scanf needs %lf, and mixing them up can give wrong results. Examples are included.

On this page
  1. Short answer
  2. Printing a double with printf
  3. Reading a double with scanf
  4. Why %f is wrong for scanf with a double
  5. Other format mismatches
  6. Printing in C++ instead

Short answer

Yes, for printing. printf uses %f to print a double, and it shows six decimal places by default. For reading a value with scanf, use %lf instead. C99 and later also accept %lf in printf, where it works the same as %f, but %f is the form most people write.

Printing a double with printf

#include <stdio.h>

int main(void) {
    double value = 3.14159;

    printf("%f\n", value);     // 3.141590
    printf("%.2f\n", value);   // 3.14
    printf("%.1f\n", value);   // 3.1
    return 0;
}

The number after the dot sets the decimal places. %.2f prints two, and %f on its own prints six. You can also set a field width, so %8.3f pads the output to eight characters.

Reading a double with scanf

#include <stdio.h>

int main(void) {
    double width = 0.0;

    printf("Enter a width: ");
    if (scanf("%lf", &width) == 1) {
        printf("You entered %.2f\n", width);
    }
    return 0;
}

Two things to notice. scanf needs the address of the variable, so the & is required. And scanf returns the number of values it read, so comparing the result with 1 confirms that the input was a number.

Why %f is wrong for scanf with a double

printf and scanf handle their arguments differently. printf receives values, and a float is promoted to double on the way in, so %f works for both types. scanf receives pointers. %f tells it to write a float, so giving it the address of a double is a mismatch.

A mismatch like that is undefined behavior. In practice, the value you read is usually wrong. Some compilers warn about it, but you can’t count on a warning, so use %lf for a double every time.

Other format mismatches

  • Using %d with a double is also undefined behavior. It prints a meaningless number, so use %f, %e or %g instead.
  • Leaving out the & in scanf is undefined behavior and can crash the program.

For reference, %e and %g print the same value in different forms:

printf("%e\n", 3.14159);   // 3.141590e+00
printf("%g\n", 3.14159);   // 3.14159

Printing in C++ instead

In C++, printf still works with <cstdio>, and the same rules apply. Most C++ code uses std::cout instead, which doesn’t take format specifiers. It also reads a double without a format string, so std::cin >> value is enough. To set the decimal places on output, use std::fixed with std::setprecision:

#include <iomanip>
#include <iostream>

int main() {
    double value = 3.14159;

    std::cout << std::fixed << std::setprecision(2) << value << '\n';   // 3.14
}

The complete guide to double and float

What double Does in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Shift Operators in C++

Learn how the << and >> shift operators work in C++, with binary examples, compound forms, precedence traps, undefined cases, and safe ways to use them.