sprintf格式化问题 [英] Format problem with sprintf
问题描述
嘿那里,
我对Virtex-4上的以下代码片段有一些问题
带有基于GCC的编译器的PowerPC
char chData [6];
sprintf(& chData [0],"%+ 05.0f",-0.038f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-0.380f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-3.800f); - 我得到-0004好的
sprintf(& chData [0],"%+ 05.0f",+ 0.038f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 0.380f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 3.800f); - 我得到+ 0004好的
我希望总能得到一个长度为5的字符串,但
有时我只能得到4个字符。这是一个编译器错误吗?
在Visual Studio中,我总是得到5个字符。
谢谢
Henryk
Hey there,
I have some problems with the following code snippet on a Virtex-4
PowerPC with a GCC based compiler
char chData[6];
sprintf(&chData[0], "%+05.0f", -0.038f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -0.380f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -3.800f); --I get "-0004" ok
sprintf(&chData[0], "%+05.0f", +0.038f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +0.380f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +3.800f); --I get "+0004" ok
I would expect to get always a string with the length of 5 but
sometimes I get only 4 chars back. Is this a compiler bug?
In Visual Studio I get always 5 chars back.
Thank you
Henryk
推荐答案
" Henryk" <他************ @ gmx.dewrote in message
news:11 ****************** ****@i42g2000cwa.googlegr oups.com ...
"Henryk" <he************@gmx.dewrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
嘿那里,
我有一些问题Virtex-4上的以下代码片段
PowerPC与基于GCC的编译器
char chData [6];
sprintf(& chData [0],"%+ 05.0f",-0.038f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-0.380f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-3.800f); - 我得到-0004好的
sprintf(& chData [0],"%+ 05.0f",+ 0.038f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 0.380f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 3.800f); - 我得到+ 0004好的
我希望总能得到一个长度为5的字符串,但
有时我只能得到4个字符。这是一个编译器错误吗?
在Visual Studio中,我总是得到5个字符。
Hey there,
I have some problems with the following code snippet on a Virtex-4
PowerPC with a GCC based compiler
char chData[6];
sprintf(&chData[0], "%+05.0f", -0.038f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -0.380f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -3.800f); --I get "-0004" ok
sprintf(&chData[0], "%+05.0f", +0.038f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +0.380f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +3.800f); --I get "+0004" ok
I would expect to get always a string with the length of 5 but
sometimes I get only 4 chars back. Is this a compiler bug?
In Visual Studio I get always 5 chars back.
我也是(VC ++ 2005 Express)。也许你的编译器有一个问题。
。尝试检查其支持资源。我们这里只讨论
讨论C语言本身,而不是特定的实现。
FWIW,我已经包含了我的测试程序及其输出(看起来像
正确)下面。
-Mike
#include< string.h>
#包括< stdio.h>
int main(无效)
{
char chData [6];
sprintf(& chData [0],"%+ 05.0f",-0.038f); / * - 我得到-000 ??? * /
printf("%s \ n",chData);
sprintf(& chData [0],"%+ 05.0 f, - 0.380f); / * - 我得到-000 ??? * /
printf("%s \ n",chData);
sprintf(& chData [0],"%+ 05.0 f",-3.800f); / * - 我得到-0004 ok * /
printf("%s \ n",chData);
sprintf(& chData [0],"%+ 05.0f,+ 0.038f); / * - 我得到+ 000 ??? * /
printf("%s \ n",chData);
sprintf(& chData [0],"%+ 05.0 f,+ 0.380f); / * - 我得到+ 000 ??? * /
printf("%s \ n",chData);
sprintf(& chData [0],"%+ 05.0 f,+ 3.800f); / * - 我得到+ 0004 ok * /
printf("%s\ n",chData);
返回0;
}
输出:
-0000
-0000
-0004 >
+0000
+0000
+0004
-Mike
Me too (VC++ 2005 Express). Perhaps there is a problem with
your compiler. Try checking its support resources. We only
discuss the C language itself here, not specific implementations.
FWIW, I''ve included my test program and its output (which looks
correct) 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
Mike Wahler< mk ****** @ mkwahler.netwrote:
Mike Wahler <mk******@mkwahler.netwrote:
我也是(VC ++ 2005 Express)。也许你的编译器有一个问题。
。尝试检查其支持资源。我们只在这里讨论C语言本身,而不是特定的实现。
Me too (VC++ 2005 Express). Perhaps there is a problem with
your compiler. Try checking its support resources. We only
discuss the C language itself here, not specific implementations.
我认为从根本上询问
特定实现的行为是否符合要求;在这种情况下,答案WRT
OP'的实现大概是不。不是说VC ++应该被保持
当然是正确的标准:-)
-
C. Benson Manica |我*应该*知道我在说什么 - 如果我
cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。
I do think it''s on-topic to essentially ask whether the behavior of a
particular implementation is conformant; in this case, the answer WRT
OP''s implementation is presumably "no". Not that VC++ should be held
up as a standard of correctness of course :-)
--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.
Henryk写道:
Henryk wrote:
嘿那里,
我在Virtex-4上的以下代码片段有一些问题
PowerPC与基于GCC的编译器
Hey there,
I have some problems with the following code snippet on a Virtex-4
PowerPC with a GCC based compiler
我不喜欢知道什么是基于Gcc的编译器手段。我的gcc编译器确实没有重现你报告的行为。在任何情况下,您报告的行为都不是,b $ b,编译器的问题,而是提供的库的问题。
将C库的版本替换为不具有的版本打破了
* printf函数。
I don''t know what a "Gcc based compiler" means. My gcc compiler does
not 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.
char chData [6];
sprintf( & chData [0],"%+ 05.0f",-0.038f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-0.380f); - 我得到-000 ????
sprintf(& chData [0],"%+ 05.0f",-3.800f); - 我得到-0004好的
sprintf(& chData [0],"%+ 05.0f",+ 0.038f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 0.380f); - 我得到+ 000 ????
sprintf(& chData [0],"%+ 05.0f",+ 3.800f); - 我得到+ 0004好的
我希望总能得到一个长度为5的字符串,但
有时我只能得到4个字符。这是一个编译器错误吗?
在Visual Studio中,我总是得到5个字符。
char chData[6];
sprintf(&chData[0], "%+05.0f", -0.038f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -0.380f); --I get "-000" ???
sprintf(&chData[0], "%+05.0f", -3.800f); --I get "-0004" ok
sprintf(&chData[0], "%+05.0f", +0.038f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +0.380f); --I get "+000" ???
sprintf(&chData[0], "%+05.0f", +3.800f); --I get "+0004" ok
I would expect to get always a string with the length of 5 but
sometimes I get only 4 chars back. Is this a compiler bug?
In Visual Studio I get always 5 chars back.
以下代码(以及带有愚蠢的& 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
+ 0004
The 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格式化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!