sprintf功能 [英] sprintf function

查看:92
本文介绍了sprintf功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是c新手并尝试sprintf功能。


我写了一个测试程序试试sprintf功能和期望

输出为1.234。然而,输出什么都没有。


我在这里错过了吗?


提前感谢您的投入。


地球


#include< stdio.h>

char * toString(double d)

{

char buffer [9];

sprintf(buffer,"%lf",d);

返回缓冲区;

}


main()

{

printf("%s \ n",toString (1.234));

}

Hi all,

I am new to c and trying the sprintf function.

I have written a testing program to try the sprintf fuction and expect
the output is 1.234. However, the output shows nothing.

Am I missing sth here?

Thanks in advance for you inputs.

Earth

#include <stdio.h>
char* toString(double d)
{
char buffer[9];
sprintf(buffer, "%lf", d);
return buffer;
}

main()
{
printf("%s\n", toString(1.234));
}

推荐答案

Earth写道:
Earth wrote:
#include< stdio.h>
char * toString(double d)
{char buffer [9];
sprintf(buffer,"%lf",d) ;
返回缓冲区;

main()
{/> printf("%s \ n",toString(1.234));
}
#include <stdio.h>
char* toString(double d)
{
char buffer[9];
sprintf(buffer, "%lf", d);
return buffer;
}

main()
{
printf("%s\n", toString(1.234));
}




当您从

返回toString()时,您认为缓冲[9]会发生什么?


(BTW toString不是有效的标识r)


-

Morris Dovey

美国爱荷华州西得梅因

C链接在 http://www.iedu.com/c

读我的嘴唇:苹果离树不远。



What do you suppose happens to buffer[9] when you return from
toString()?

(BTW toString is not a valid identifier)

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn''t fall far from the tree.


文章< ab ******** ******************@posting.google.com> ;, ea *****@yahoo.com.hk (地球)写道:
In article <ab**************************@posting.google.com >, ea******@yahoo.com.hk (Earth) wrote:
我写了一个测试程序来试试sprintf功能并期待
输出为1.234。但是,输出显示什么。
#include< stdio.h>
char * toString(double d)
{char buffer [9];
sprintf(buffer,"%lf" ;,d);
返回缓冲区;
}
I have written a testing program to try the sprintf fuction and expect
the output is 1.234. However, the output shows nothing. #include <stdio.h>
char* toString(double d)
{
char buffer[9];
sprintf(buffer, "%lf", d);
return buffer;
}




问题是''​​缓冲区''是toString函数的本地。


参见 http://www.eskimo.com/~scs/C-faq/q7.5.html

Ian。



The problem is with ''buffer'' being local to the toString function.

See http://www.eskimo.com/~scs/C-faq/q7.5.html

Ian.


Earth写道:
Earth wrote:
大家好,

我是c的新手并尝试使用sprintf函数。
我已经编写了一个测试程序来尝试sprintf功能,并期望输出为1.234。然而,输出没有显示任何内容。

我在这里错过了吗?

提前感谢您的投入。

地球

#include< stdio.h>
char * toString(double d)
{char buffer [9];
sprintf(buffer,"%lf" ,d);
返回缓冲区;

main()
{/> printf("%s \ n",toString(1.234) ));
}
Hi all,

I am new to c and trying the sprintf function.

I have written a testing program to try the sprintf fuction and expect
the output is 1.234. However, the output shows nothing.

Am I missing sth here?

Thanks in advance for you inputs.

Earth

#include <stdio.h>
char* toString(double d)
{
char buffer[9];
sprintf(buffer, "%lf", d);
return buffer;
}

main()
{
printf("%s\n", toString(1.234));
}




恭喜!!你点了FAQ大奖。事实上,我们甚至有一个FAQ

,它实际上在回答你的问题时使用了sprintf。您可以通过以下方式找到这个问题和答案:

http://www.eskimo.com/~scs/C-faq/q7.5.html


请花时间浏览精美的常见问题解答。它可以在:

http://www.eskimo.com/~scs/C-faq/top.html

-nrk。


-

删除电子邮件的devnull



Congratulations!! You hit the FAQ jackpot. In fact, we even have an FAQ
that actually uses sprintf while answering your very question. You can
find this question and answer at:

http://www.eskimo.com/~scs/C-faq/q7.5.html

Please to take the time to go through the fine FAQ. It''s available at:

http://www.eskimo.com/~scs/C-faq/top.html

-nrk.

--
Remove devnull for email


这篇关于sprintf功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆