[问]在这种情况下strcpy和sprintf有什么不同 [英] [Q]What is different between strcpy and sprintf in this case
问题描述
嗨
有人可以告诉我下面的情况有什么不同吗?
strcpy(eventname," MDCX_RSP");
和
sprintf(eventname," MDCX_RSP");
谢谢,
Leon < br>
Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");
Thanks,
Leon
推荐答案
2003年11月27日19:18:11 -0800, io****@yahoo.com (ios)写道:
On 27 Nov 2003 19:18:11 -0800, io****@yahoo.com (ios) wrote:
嗨
有人可以告诉我下面的案例有什么不同吗?
strcpy(eventname," MDCX_RSP");
和
sprintf(eventname," MDCX_RSP");
Hi
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");
Were你期待什么?
eventname的内容(如果是数组)或内存的内容
指向(如果指针)将是对于
这个例子中的任何一个陈述都是一样的。如果第二个参数包含
,那么sprintf会考虑转换规范,这是不正确的。
strcpy返回指针而sprintf返回一个int但是,因为
要么被丢弃,我不认为这是一个相关的区别。
我希望strcpy明显更快但这是我的
直观的判断而不是标准的一部分。
我能看到的唯一真正的区别是你需要包含一个
根据您使用的不同头文件。
<<删除电子邮件的del>>
Were you expecting any?
The contents of eventname (if an array) or the contents of the memory
it points to (if a pointer) will be the same for either statement in
this example. This would not be true if the second argument contained
anything sprintf would consider a conversion specification.
strcpy returns a pointer while sprintf returns an int but, since
either will be discarded, I don''t think this is a relevant difference.
I would expect strcpy to be significantly faster but this is my
intuitive judgement and not part of the standard.
The only real difference I can see is that you need to include a
different header file depending on which you use.
<<Remove the del for email>>
On Thu,2003年11月27日,ios写道:
On Thu, 27 Nov 2003, ios wrote:
[顺便说一下,没有必要用[Q]标记你的问题;
即使是最近的趋势,*是*默认值。
我能想到的唯一公认的主题标签clc
是[OT] ,标记偏离主题的帖子。]
有人可以告诉我下面的案例有什么不同吗?
strcpy(eventname," MDCX_RSP");
和
sprintf(eventname," MDCX_RSP");
[Incidentally, there''s no need to tag your question with [Q];
that *is* the default around here, despite recent tendencies.
The only recognized subject-line tag I can think of for c.l.c
is [OT], marking off-topic posts.]
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");
在这种情况下,绝对没有。但一般来说,
sprintf()有整个''格式说明符''行李,比如
printf()呢;当然sprintf()返回一个字符
count其中strcpy()返回一个指向目标的指针
数组。
特别注意
strcpy(d,s);
和
sprintf(d,"%s",s);
在所有情况下完全相同,
strcpy(d,s);
和
sprintf(d,s);
不相同;考虑
(0 == strcmp(s," %%"))的情况。
HTH,
-Arthur
In this case, absolutely nothing. In general, though,
sprintf() has the whole ''format specifiers'' baggage, like
printf() does; and of course sprintf() returns a character
count where strcpy() returns a pointer to the destination
array.
Note particularly that while
strcpy(d,s);
and
sprintf(d,"%s",s);
are exactly identical under all circumstances,
strcpy(d,s);
and
sprintf(d,s);
are NOT identical; consider the case where
(0 == strcmp(s,"%%")).
HTH,
-Arthur
ios写道:
有人能告诉我下面的情况有什么不同吗?
strcpy(eventname," MDCX_RSP");
和
sprintf(eventname," MDCX_RSP");
Can someone tell me what is different between below case?
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");
我相信strcpy会复制最终的''\0'',而sprintf将不会复制
。因此,您几乎肯定想要获得sprintf的返回值
。
-
查克F(cb *** *****@yahoo.com)(cb********@worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>使用worldnet地址!
I believe that strcpy will copy the final ''\0'', and sprintf will
not. Thus you almost certainly want to capture the return value
of sprintf.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
这篇关于[问]在这种情况下strcpy和sprintf有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!