printf();

山在岭就在 2014-05-21 09:28:24
y=333.1234567;
printf("%2.5f",y);
最后输出结果是多少呀?不是只占两个宽度吗?那怎么显示小数点后有5位
...全文
177 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
li4c 2014-05-22
  • 打赏
  • 举报
回复
楼主为什么不自己试试呢,运行了就知道了
赵4老师 2014-05-22
  • 打赏
  • 举报
回复
printf Width Specification The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result. Precision Specification The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table R.5). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below: printf( "%.0d", 0 ); /* No characters output */ If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list. The type determines the interpretation of precision and the default when precision is omitted, as shown in Table R.5. Table R.5 How Precision Values Affect Type Type Meaning Default c, C The precision has no effect. Character is printed. d, i, u, o, x, X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. Default precision is 1. e, E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed. f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed. g, G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s, S The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered. If the argument corresponding to a floating-point specifier is infinite, indefinite, or NaN, printf gives the following output. Value Output + infinity 1.#INFrandom-digits – infinity –1.#INFrandom-digits Indefinite (same as quiet NaN) digit.#INDrandom-digits NAN digit.#NANrandom-digits
xuzhouweihao 2014-05-22
  • 打赏
  • 举报
回复
引用 1 楼 cao_julians 的回复:
%m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)
y=333.1234567; printf("%8.5f\n",y); 这样的话 8>5+2 但结果显示为 333.12346 加上小说点是9位了。 这个也是属于相同意思的编译器行为吗?
shlvshe00 2014-05-22
  • 打赏
  • 举报
回复
引用 1 楼 cao_julians 的回复:
%m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)
++
zllhy1215 2014-05-22
  • 打赏
  • 举报
回复
实际数值大于给定宽度,按实际输出!
juelianhuayao 2014-05-22
  • 打赏
  • 举报
回复
引用 3 楼 zxh707wk 的回复:
引用 2 楼 turingo 的回复:
++ [quote=引用 1 楼 cao_julians 的回复:] %m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)
+1[/quote] ++
707wk 2014-05-22
  • 打赏
  • 举报
回复
引用 2 楼 turingo 的回复:
++
引用 1 楼 cao_julians 的回复:
%m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)
+1
图灵狗 2014-05-21
  • 打赏
  • 举报
回复
++
引用 1 楼 cao_julians 的回复:
%m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)
cao_julians 2014-05-21
  • 打赏
  • 举报
回复
%m.nf格式中m和n的关系应该是m>n+2,即没有整数部分的位置,也要考虑一个符号位和一个小数点位。你的%2.5f对编译器而言本来就是不合理要求,它只能按它能满足的某个功能:如保证值的正确性、保证值的有效宽度等----这些称作编译器的行为(实现的行为这个编译器的作者在满足ANSI C的大前提下,个别ANSI C没有严格限定的就按作者自己的意愿做了)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧