-1

I am learning the language C. I am trying to print a set of numbers in the datatype "double" but it is only printing 7 digits like "float".

For example:

double temp = 23.3456789112345;

printf("%1f\n", temp);

Outputs this:

23.345679

However, once i changed where the decimal is like the following:

double temp = 2334567.89112345;

printf("%1f\n", temp);

It outputed this:

2334567.891123

SO it almost worked. Instead of outputing only 7 digits, it printed 13 digits. But I thought the datatype "double" could print 15-16 digits .

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Six digits after the decimal point is "default". Review "format specifiers" doco for `printf()`. You can get more (or fewer) digits if you ask for them to be printed. (Experiment, and notice that the number will be correctly rounded, too.) – Fe2O3 Feb 23 '23 at 09:26
  • What is the point of `1` in `%1f` ? – M. Nejat Aydin Feb 23 '23 at 09:30
  • 2
    @M.NejatAydin Possibly a typo meant to say`%lf`? Some dysfunctional fonts unsuitable for programming have the same symbol for `1` (one) and `l` (L). – Lundin Feb 23 '23 at 09:53

1 Answers1

3

From the C Standard (7.21.6.1 The fprintf function)

f,F A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

So for example you could write

double temp = 23.3456789112345;

printf("%.16f\n", temp);

Pay attention to that the length modifier l in %lf (I think you wanted to use %lf instead of %1f) is redundant and has no effect.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335