Thread: Explanation of a function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    Explanation of a function

    Hi, can someone tell me, in extended terms what this function parameters are

    what does char* buf, const .... etc mean


    int sprintf ( char *buf, const char *format, ... );
    int snprintf( char *buf, size_t n, const char *format, ... );


    like printf has its format... (string ,then variable)

    whats sprintfs format.

    Thanks

    Greg

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    printf:
    Code:
    printf ( "%d\n", 10 );
    sprintf:
    Code:
    char buf[4];
    
    sprintf ( buf, "%d\n", 10 );
    snprintf:
    Code:
    char buf[4];
    
    snprintf ( buf, 4, "%d\n", 10 );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    35

    Wink

    I've found good use for sizeof() in such functions as snprintf. You never know when you're going to resize the buffer a little, and when you do, it can be a real pain to dig up all those hardcoded size limiters.


    Code:
    char buf[4];
    
    snprintf ( buf, sizeof(buf), "%d\n", 10 );

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    I've seen many functions (or maybe its an un-official convention) when ever you see an 'n' in a function name, you can guess that the function involves a parameter for 'Number of Items/characters/etc'.

    sprintf() is used when you want to output the values of variables into a character array/buffer. Its just like printf except that instead of the usual STDOUT, it outputs to the character buffer/arrray.

    Have Fun
    Help everyone you can

  5. #5
    Quote Originally Posted by MortalMonkey
    I've found good use for sizeof() in such functions as snprintf. You never know when you're going to resize the buffer a little, and when you do, it can be a real pain to dig up all those hardcoded size limiters.
    Code:
    char buf[4];
    snprintf ( buf, sizeof(buf), "%d\n", 10 );
    It's fine ... as long as 'buf' is an array. Just keep in mind that an array decays to a pointer when passed to a function. An extra parameter for the size could be necessary for flexibility.
    Last edited by Emmanuel Delaha; 06-20-2004 at 01:20 AM. Reason: wording
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by MortalMonkey
    I've found good use for sizeof() in such functions as snprintf. You never know when you're going to resize the buffer a little, and when you do, it can be a real pain to dig up all those hardcoded size limiters.


    Code:
    char buf[4];
    
    snprintf ( buf, sizeof(buf), "%d\n", 10 );
    This is a rather old thread...
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Yeah sorry, but it was the 4th or 5th down when searching for "snprintf". Quite a shame, really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 18
    Last Post: 12-31-2005, 01:56 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM