Tho*_*s O 0 c floating-point printf
我正在尝试使用以下代码printf/sprintf浮点数:
sprintf(buff, "FPS: %d\n%.4f N %.4f E\nAl: %.1fm Rl: %.1f\n", fps, p_viewer.p.lat, p_viewer.p.lon, p_viewer.p.alt, p_viewer.roll);
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我会收到这些警告:
gfx_game_engine.c:300: warning: format '%.4f' expects type 'double', but argument 4 has type 'float'
gfx_game_engine.c:300: warning: format '%.4f' expects type 'double', but argument 5 has type 'float'
gfx_game_engine.c:300: warning: format '%.1f' expects type 'double', but argument 6 has type 'float'
gfx_game_engine.c:300: warning: format '%.1f' expects type 'double', but argument 7 has type 'float'
Run Code Online (Sandbox Code Playgroud)
什么是sprintf浮动的正确方法?是否有特殊的格式字符?我觉得编译器可能会以某种方式转换类型,这可能会导致它减慢速度.
我正在使用dsPIC33FJ128GP802微控制器并使用MPLAB C30进行编译,MPLAB C30是GCC v3.23的变体.
你的gcc变种坏了.在C语言中,无法将a传递float给可变参数函数.小型(char,short和float)的默认促销至少int/ double始终适用.我猜这个微控制器攻击gcc的人做了什么来禁用促销活动,可能是因为这个想法double很慢或很难通过.你应该把它们焚烧到地狱和背部.如果编译器供应商不想double正确支持,那么正确的解决方案是使所有浮点类型具有相同的大小和等价float,而不是违反语言的提升规则.
另一个想法:原型也可能sprintf缺失或错误,例如,int sprintf();而不是int sprintf(char *, const char *, ...);