C语言中%5.2f中的5和.2都分别代表什么意思?求详细解释!

如题所述

%[flags] [width] [.precision] [{h | l | ll | w | I | I32 | I64}] type

%                5      .2                          f

最小宽度为5,精度到小数点2位的浮点数格式输出。

详情请看下面:

Format Specification Syntax: printf and wprintf Functions

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Format Specification Syntax: printf and wprintf Functions.

Describes the syntax for format string arguments to printf, wprintf, and related functions. More secure versions of these functions are available; for more information, see Security Features in the CRT. For information about the individual functions, see the documentation for those specific functions. For a listing of these functions, see Stream I/O.

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h | l | ll | w | I | I32 | I64}] type

Each field of the format specification is a character or a number that signifies a particular format option or conversion specifier. The required type character specifies the kind of conversion to be applied to an argument. The optional flags, width, and precision fields control additional format aspects. A basic format specification contains only the percent sign and a type character—for example, %s, which specifies a string conversion. In the secure versions of the functions, if a percent sign is followed by a character that has no meaning as a format field, the invalid parameter handler is invoked. For more information, see Parameter Validation. In the non-secure versions, the character is copied to the output unchanged. To print a percent-sign character, use %%.

The fields of the format specification control the following aspects of argument conversion and formatting:

type
Required conversion specifier character that determines whether the associated argument is interpreted as a character, a string, an integer, or a floating-point number. For more information, see printf Type Field Characters.

flags
Optional character or characters that control output justification and output of signs, blanks, leading zeros, decimal points, and octal and hexadecimal prefixes. For more information, see Flag Directives. More than one flag can appear in a format specification, and flags can appear in any order.

width
Optional decimal number that specifies the minimum number of characters that are output. For more information, see printf Width Specification.

precision
Optional decimal number that specifies the maximum number of characters that are printed for strings, the number of significant digits or the number of digits after the decimal-point character for floating-point values, or the minimum number of digits that are printed for integer values. For more information, see "How Precision Values Affect Type" in Precision Specification.

h | l | ll | w | I | I32 | I64
Optional prefixes to type that specify the size of the corresponding argument. For more information, see"Size Prefixes" in Size Specification.

温馨提示:内容为网友见解,仅供参考
第1个回答  2016-01-12
整数5位,小数2位,整数不够5位时以空格点位。本回答被网友采纳
第2个回答  推荐于2018-03-02

本回答被提问者和网友采纳
相似回答