Home > Backend Development > C++ > body text

The difference between f and lf in C language

下次还敢
Release: 2024-04-29 21:36:17
Original
806 people have browsed it

In C language, the f format specifier prints the decimal format of a floating-point variable, retaining 6 significant digits after the decimal point; the lf format specifier prints the long decimal format, retaining 12 significant digits after the decimal point.

The difference between f and lf in C language

The difference between f and lf in C language

In C language, f and lf are format specifiers used to specify the format used when printing floating-point variables. Their specific differences are as follows:

f Format specifier

  • Print the decimal format of floating point variables, retaining 6 digits after the decimal point effective number.
  • Use decimal point as decimal separator by default.
  • If the field width is not specified, the printing width is the minimum width sufficient to accommodate the printed value.

lf Format specifier

  • Print the long decimal format of floating point variables, retaining 12 digits after the decimal point. number.
  • Use decimal point as decimal separator by default.
  • If the field width is not specified, the printing width is the minimum width sufficient to accommodate the printed value.

Example

<code class="c">#include <stdio.h>

int main()
{
    float value = 3.14159265;

    printf("f: %.2f\n", value);
    printf("lf: %.12lf\n", value);

    return 0;
}</code>
Copy after login

Output:

<code>f: 3.14
lf: 3.141592650000</code>
Copy after login

As can be seen from the output:

  • Using f When the format specifier prints floating-point variables, 2 significant digits after the decimal point are retained.
  • When printing floating-point variables using the lf format specifier, 12 significant digits after the decimal point are retained.

The above is the detailed content of The difference between f and lf in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!