Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C] Sprintf example and explanation
06-01-2010, 10:37 AM
Post: #1
[C] Sprintf example and explanation
Code:
/**
*Sprintf example and explanation
*[C] Back_Track
*http://www.pro9ramming.com
*
**/


#include <stdio.h>

int main(int argc, char *argv[])
    {
        /*
        *sprintf_data will  store sprintf, sprintf returns and int(length of the string)
        *adding_data will be used as data to add into our string
        */
        int sprintf_data , adding_data=95345;
        /*
        *String_Sprintf is the String we'll add the characters into
        */
        char  String_Sprintf[256];
        /*
        *make the int sprintf_data be equal to the number of Chars in the String String_sprintf
        *The first variable is where the next parameter will be stored(the string)
        *the third argument is just for the %d in the string in the 2nd parameter
        */
        sprintf_data = sprintf(String_Sprintf,"%d Here is a string.",adding_data);
        /*
        *Print it
        */
        printf("Length of String_Sprintf  is %d\nIt contains\n%s\n",sprintf_data,String_Sprintf);
        
        return 0;
    }

usage:
int sprintf ( char * str, const char * format, ... );

Sprintf just formats a String, and at the same time, it gives the length value of the String to a previously initiated integer.

This works like printf,but instead of writing to the console, it writes to a String

Referenced
C++ Reference helped me to understand the core concept of this.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
06-02-2010, 03:15 AM
Post: #2
RE: [C] Sprintf example and explanation
Very good explanation i can say !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: