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

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 11/28/03


Date: 28 Nov 2003 04:04:36 GMT

On 27 Nov 2003 19:18:11 -0800, ioswan@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>>



Relevant Pages

  • Re: String Concatenation Using Pointers No strcat()
    ... The signature for strcpy() is: ... pointer to the function. ... You can't pass an array into any function. ... dest = malloc+1; ...
    (comp.lang.c)
  • Re: strcpy question
    ... definition for it's version of strcpy ... according to the appendix, returns the target string. ... original string and a pointer to the target string? ... You can't pass an array. ...
    (comp.lang.c)
  • Re: check return value of strcpy()
    ... The return value of strcpy() is a convenience only, allowing the address of the target to be an expression and the return value to be the result of evaluating that expression. ... I don't think that any of these return values are used to represent an error condition (although the null pointer from strchr, strrchr, strstr might be interpreted as such), so "checking" the return value is usually a meaningless term. ... Please note that "NUL" is _not_ a null pointer. ... And, no matter how much you think other people's judgments unimportant, they can matter. ...
    (comp.lang.c)
  • Re: Two Questions about "strlen", "strcat" and "strcpy"
    ... sprintf might implement %s with a strcpy and a strlen ... so the sprintf solution need not be as efficient as it looks. ... > be, possibly by far, the best solution for multiple string concatenation. ...
    (comp.lang.c)
  • Re: Question on arrays within a struct and sprintf
    ... mainreturns int, not void. ... implicitly converted to a pointer to its first element *unless* it's ... The first call to sprintf is ok. ...
    (comp.lang.c)