By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

[Q]What is different between strcpy and sprintf in this case

P: n/a
ios
Hi

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");

Thanks,
Leon
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 27 Nov 2003 19:18:11 -0800, io****@yahoo.com (ios) wrote:
Hi

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");

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>>
Nov 13 '05 #2

P: n/a

On Thu, 27 Nov 2003, ios wrote:

[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");


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

Nov 13 '05 #3

P: n/a
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


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!
Nov 13 '05 #4

P: n/a
"CBFalconer" <cb********@yahoo.com> wrote:
ios wrote:
strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


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.


sprintf will write a '\0' to terminate the output string.

There is no difference between the two given statements,
assuming that both <stdio.h> and <string.h> are #included.

--
Simon.
Nov 13 '05 #5

P: n/a
Simon Biber wrote:
"CBFalconer" <cb********@yahoo.com> wrote:
ios wrote:
> strcpy(eventname, "MDCX_RSP");
> and
> sprintf(eventname, "MDCX_RSP");


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.


sprintf will write a '\0' to terminate the output string.

There is no difference between the two given statements,
assuming that both <stdio.h> and <string.h> are #included.


You are correct (and I think Chuck was just hallucinating or something), but
it bears repeating (assuming someone already pointed it out) that the
strcpy version is likely to have superior performance, and that the sprintf
could break if a different string literal is used. For example,
sprintf(eventname, "%saved"); /* !!! */

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6

P: n/a
In article <3F***************@yahoo.com>,
CBFalconer <cb********@yahoo.com> wrote:
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


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.


They both will copy the final '\0'. The difference is what happens when
a maintenance programmer has a reason to change "MDCX_RSP" to something
else, for example something that contains % characters, and doesn't
notice that sprintf has been used...

So the second form is a bug waiting to come out and byte you where it
hurts. (The exception would be if this is within a series of sprintf
statements, where some don't have any additional arguments except the
formatting string).
Nov 13 '05 #7

P: n/a
there is no difference between the two cases. However if you want to
include some integer in between you can't use strcpy

io****@yahoo.com (ios) wrote in message news:<79*************************@posting.google.c om>...
Hi

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");

Thanks,
Leon

Nov 13 '05 #8

P: n/a
ios <io****@yahoo.com> wrote:
Hi

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


No difference.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #9

P: n/a
In <3F***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
ios wrote:

Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


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.


Don't be idiot! What do you think the 's' in sprintf stands for?

2 The sprintf function is equivalent to fprintf, except that the
output is written into an array (specified by the argument s)
rather than to a stream. A null character is written at the end
of the characters written; it is not counted as part of the
returned value.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

P: n/a
In <79*************************@posting.google.com> io****@yahoo.com (ios) writes:

First, there is no point in using the [Q] tag in the subject line when
posting a question. A question mark at the end is a better choice.
Can someone tell me what is different between below case?

strcpy(eventname, "MDCX_RSP");
and
sprintf(eventname, "MDCX_RSP");


They have the same effect, only the return value (which you ignore,
anyway) is different.

However, the execution of the strcpy call is likely to be faster than the
execution of the sprintf call. The former has to compare each copied
character to 0, while the latter has to compare it to both 0 and % and
also check each character whether it is a single byte character of the
first byte of a multibyte character.

To be perfectly safe, regardless of the contents of the copied string,
the sprintf call should be written like this:

sprintf(eventname, "%s", "MDCX_RSP");

but why bother, since strcpy() is the right tool for the job?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.