Thread: question about sprintf()

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    question about sprintf()

    I have another simple question.

    say for example my code looks like this:

    Code:
    char str[10] = "abcdef";
    sprintf(str, "%c", 'g');
    what will happen to the original contents of str? will g be appended to them or will they be cleared?


    thank you in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you compile it and find out? Actually all you have to do is put a few seconds of thought into it. Or you could just hilight your function call to sprintf in the IDE you're posting in your sig, and press something like F1, and get the detailed help on the function itself. Assuming of course you have installed the MSDN installed. Surely with your boast of the Enterprise Edition, you have the MSDN. I mean it's not like you pirated it or anything, right?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    lol

    I checked msdn, it doesn't answer my question.

    the below printed g on the screen, so I guess it clears the original contents.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char str[10] = "abcdef";
    
       sprintf(str, "%c", 'g');
       printf(str);
    
       return 0;
    }
    sorry, I won't post my very simple questions here again.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't clear it. It copies what you tell it to into the array. Since it's a string, it terminates it with a null character. Print the array a character at a time and you'll see it doesn't get cleared at all.

    It (the MSDN) does tell you what it (sprinft) does. Read:
    The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.
    You can post simple questions, it's just that you'll find it often faster, and a better learning experience, to try it yourself.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to append to the string, try:
    Code:
    char str[10] = "abcdef";
    sprintf(str+strlen(str), "%c", 'g');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. sprintf question
    By aaronc in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 11:11 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM