Thread: Sprintf

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Question Sprintf

    what exactly does the sprintf() function?? I use it in my game and works fine, but i dont really know how it works.
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    doesnt it combine things to make 1 variable?

    like:

    int a = 100;
    char buffer[256];
    buffer = sprintf("This is a number: %n", a);
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    It takes a similiar input as printf, but instead of writing it to the screen it writes it to a string.
    Code:
    char Buffer[256];
    sprintf(Buffer, "This is a string: %s\tInteger: %d\tFloat: %f", String, Int, Float);
    sprintf can write past the end of the string (in this case it is 256). In order to avoid that you can use snprintf.
    Code:
    char Buffer[256]
    snprintf(Buffer, 256, "This is a string: %s\tInteger: %d\tFloat: %f", String, Int, Float);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

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. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM