+0000 +0000 +0004 -MikeMe too (VC++ 2005 Express). Perhaps there is a problem withyour compiler. Try checking its support resources. We onlydiscuss the C language itself here, not specific implementations.FWIW, I''ve included my test program and its output (which lookscorrect) below.-Mike#include <string.h>#include <stdio.h>int main(void){char chData[6];sprintf(&chData[0], "%+05.0f", -0.038f); /* --I get "-000" ??? */printf("%s\n", chData);sprintf(&chData[0], "%+05.0f", -0.380f); /* --I get "-000" ??? */printf("%s\n", chData);sprintf(&chData[0], "%+05.0f", -3.800f); /* --I get "-0004" ok */printf("%s\n", chData);sprintf(&chData[0], "%+05.0f", +0.038f); /* --I get "+000" ??? */printf("%s\n", chData);sprintf(&chData[0], "%+05.0f", +0.380f); /* --I get "+000" ??? */printf("%s\n", chData);sprintf(&chData[0], "%+05.0f", +3.800f); /* --I get "+0004" ok */printf("%s\n", chData);return 0;}Output:-0000-0000-0004+0000+0000+0004-Mike 我认为从根本上询问 特定实现的行为是否符合要求;在这种情况下,答案WRT OP'的实现大概是不。不是说VC ++应该被保持 当然是正确的标准:-) - C. Benson Manica |我*应该*知道我在说什么 - 如果我 cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。I do think it''s on-topic to essentially ask whether the behavior of aparticular implementation is conformant; in this case, the answer WRTOP''s implementation is presumably "no". Not that VC++ should be heldup as a standard of correctness of course :-)--C. Benson Manica | I *should* know what I''m talking about - if Icbmanica(at)gmail.com | don''t, I need to know. Flames welcome. 我不喜欢知道什么是基于Gcc的编译器手段。我的gcc编译器确实没有重现你报告的行为。在任何情况下,您报告的行为都不是,b $ b,编译器的问题,而是提供的库的问题。 将C库的版本替换为不具有的版本打破了 * printf函数。I don''t know what a "Gcc based compiler" means. My gcc compiler doesnot reproduce the behavior you report. The behavior you report is not,in any case, a question of the compiler but of a supplied library.Replace your version of the C library with one that does not have broken*printf functions. 以下代码(以及带有愚蠢的& chData [0]" for plain " chData" )用gcc给出它后面的输出。 #include< stdio.h> int main(无效) { char chData [6]; sprintf(chData,"%+ 05.0f",-0.038f); printf("%s \ n",chData); sprintf(chData,"%+ 05.0f",-0.380f); printf("%s \ n",chData); sprintf(chData,"%+ 05.0f",-3.800f); printf( "%s \ nn",chData); sprintf(chData,"%+ 05.0f",+ 0.038f); printf("%) s \ n",chData); sprintf(chData,"%+ 05.0f",+ 0.380f); printf("%s \ n",chData); sprintf(chData,"%+ 05.0f",+ 3.800f); printf("%s \ n", chData); 返回0; } -00 00 -0000 -0004 +0000 +0000 + 0004The following code (and a version with the silly "&chData[0]" for plain"chData") give, with gcc, the output following it.#include <stdio.h>int main(void){char chData[6];sprintf(chData, "%+05.0f", -0.038f);printf("%s\n", chData);sprintf(chData, "%+05.0f", -0.380f);printf("%s\n", chData);sprintf(chData, "%+05.0f", -3.800f);printf("%s\n", chData);sprintf(chData, "%+05.0f", +0.038f);printf("%s\n", chData);sprintf(chData, "%+05.0f", +0.380f);printf("%s\n", chData);sprintf(chData, "%+05.0f", +3.800f);printf("%s\n", chData);return 0;}-0000-0000-0004+0000+0000+0004 这篇关于sprintf格式化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 05:49