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

I

ios

Hi

Can someone tell me what is different between below case?

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

Thanks,
Leon
 
A

Arthur J. O'Dwyer

[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
 
B

Barry Schwarz

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>>
 
C

CBFalconer

ios said:
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.
 
S

Simon Biber

CBFalconer said:
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.
 
R

Richard Heathfield

Simon said:
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"); /* !!! */
 
C

Christian Bau

CBFalconer said:
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).
 
V

vadi

there is no difference between the two cases. However if you want to
include some integer in between you can't use strcpy
 
D

Dan Pop

In said:
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
 
D

Dan Pop

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top