printf();

山在岭就在 2014-05-21 09:28:24
y=333.1234567;
printf("%2.5f",y);
最后输出结果是多少呀?不是只占两个宽度吗?那怎么显示小数点后有5位
...全文
187 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 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没有严格限定的就按作者自己的意愿做了)
一、 数据概览 塔里木内流区流经空间范围SHP矢量数据是一套以矢量格式精确描绘塔里木内流区(又称塔里木盆地内流区)地理边界的地理信息系统(GIS)基础数据。该数据以业界通用的Shapefile(.shp)格式存储,定义了塔里木内流区这一完整、封闭的自然地理单元的空间范围。它本质上是一个多边形矢量图层,其边界线勾勒出了所有最终流入塔里木盆地内部而不汇入海洋的水系的集水区域。 该数据是研究中国西北干旱区水文、生态、气候及资源环境的核心基础数据之一,为相关领域的科学分析、规划决策提供了精确的空间框架。 二、 地理范围与重要性 塔里木内流区是中国最大的内流区,也是世界著名的干旱中心。其空间范围大致涵盖新疆维吾尔自治区南部的塔里木盆地及其周边山脉(天山、昆仑山、阿尔金山、帕米尔高原)的集水区域。 核心区域:数据范围主要包括塔里木河干流及其源流——阿克苏河、叶尔羌河、和田河、开都-孔雀河等流域的集水区。 地理特征:该区域地形封闭,四周高山环绕,降水主要来自山区,冰川融水是河流的重要补给来源。所有河流均向盆地中心汇集,最终消失于沙漠或汇入尾闾湖(如历史上的罗布泊)。 生态与经济重要性:塔里木内流区是南疆地区的生命线。著名的塔里木河是中国最长的内流河,其沿岸形成的天然植被带(“绿色走廊”)是阻挡塔克拉玛干沙漠和库鲁克沙漠合拢的关键生态屏障。同时,该区域也是重要的棉花、林果业和能源基地。 三、 数据内容与属性 SHP格式数据通常由多个文件组成(如 .shp, .shx, .dbf等),其中包含空间几何信息和属性信息。 空间几何:数据以一个或多个多边形(Polygon)要素构成,每个多边形代表塔里木内流区边界内的一块连续区域。其坐标系统通常采用地理坐标系(如WGS84)或投影坐标系(如Albers等积投影),以确保空间量算的准确性。 属性表:属性表(.dbf文件)记录了该
内容概要:本文深入探讨了Redis高级数据结构在分布式系统与实时应用中的核心作用,重点围绕String、Hash、ZSet和HyperLogLog四种数据结构,结合分布式锁与实时排行榜两大典型场景进行实战解析。通过Python + FastAPI + Redis的代码实现,详细展示了如何利用ZSet的原子性操作(如ZINCRBY、ZREVRANK、ZREVRANGE)构建高性能的实时积分排行榜,并分析其时间复杂度、内存占用及扩展功能。同时,文章还介绍了分布式锁的实现原理及其在秒杀场景中防止超卖的应用。; 适合人群:具备一定Redis基础,熟悉基本数据结构,从事后端开发或系统架构工作1-3年的研发人员;正在参与高并发、实时性要求较高的项目开发者;希望深入理解Redis在实际业务中高级应用的技术人员。; 使用场景及目标:①掌握如何使用ZSet实现高效的实时排行榜,支持动态更新与TOP N查询;②理解分布式锁的实现机制及其在库存扣减、防并发冲突中的应用;③学习Redis在去重统计(UV)、对象存储(Hash)等场景的最佳实践;④提升对Redis原子操作、性能优化和生产环境部署的认知。; 阅读建议:建议结合代码动手实践,重点关注ZSet的核心命令与原子性保障机制,在本地搭建Redis环境运行示例程序并模拟高并发测试,同时延伸学习Redis模块化生态(如RedisBloom、RedisTimeSeries)以拓展技术视野。

70,027

社区成员

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

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