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

Discussion in 'C Programming' started by ios, Nov 28, 2003.

  1. ios

    ios Guest


    Can someone tell me what is different between below case?

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

    ios, Nov 28, 2003
    1. Advertisements

  2. [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.]
    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
    Note particularly that while


    are exactly identical under all circumstances,


    are NOT identical; consider the case where
    (0 == strcmp(s,"%%")).

    Arthur J. O'Dwyer, Nov 28, 2003
    1. Advertisements

  3. 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>>
    Barry Schwarz, Nov 28, 2003
  4. ios

    CBFalconer Guest

    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.
    CBFalconer, Nov 28, 2003
  5. ios

    Simon Biber Guest

    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 Biber, Nov 28, 2003
  6. 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, Nov 28, 2003
  7. 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).
    Christian Bau, Nov 28, 2003
  8. ios

    vadi Guest

    there is no difference between the two cases. However if you want to
    include some integer in between you can't use strcpy
    vadi, Nov 28, 2003
  9. ios

    Eric Guest

    No difference.
    Eric, Nov 28, 2003
  10. ios

    Dan Pop Guest

    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 Pop, Nov 28, 2003
  11. ios

    Dan Pop Guest

    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.
    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 Pop, Nov 28, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.