sscanf and sprintf functions

sscanf() function is used to extract strings from the given string.

Consider,

char *str = "Learn C Online";

If we want to extract “Learn”, “C” and “Online” in a different variable then it can be done using sscanf function.

Syntax:
sscanf(characterArray, “Conversion specifier”, address of variables);
This will extract the data from the character array according to the conversion specifier and store into the respective variables.
sscanf() will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab).

Let us understand this using an example.

char *str = "Learn C Online";
char *first, *second, *third;
sscanf(str, "%s %s %s",first,second,third);

In the above example,
“Learn” will get stored in variable first
“C” will get stored in variable second
“Online” will get stored in variable third.

sprintf() function is exactly opposite to sscanf() function. Sprint() function writes the formatted text to a character array.

Syntax:
sprintf (CharacterArray,”Conversion Specifier”, variables);

Let us understand this using an example.

char *str;
char *first = "Learn", *second = "C", *third = "Online";
sprintf(str, "%s %s %s",first,second,third);

In the above example, “Learn C Online” will get stored in the character array str.

You may also like...

19 Responses

  1. hai says:

    excellent.
    sir,please provide a complete example programe with output.

  2. jjay says:

    It makes it clearer for beginners if you use separate lines and I agree with hai’s comment about a minimal working example.

    char *str;
    char *first = “Learn”;
    char *second = “C”;
    char *third = “Online”;
    sprintf(str, “%s %s %s”,first,second,third);

  3. Saloni says:

    Hi while trying to run this it returns a error ‘ Segmentation fault’ please help

  4. Sarah says:

    EXCELLENT!

  5. Good Explaination…

  6. Karthik D says:

    add
    str=(char *)malloc(strlen(first)+strlen(second)+strlen(third)+3);
    before sprintf statement to remove segmentation fault error

  7. C_devil says:

    excellent work…keep it up

  8. C_devil says:

    excellent work…thankyou

  9. cant get this to work???

    char *str = “Learn C Online”;
    char *first, *second, *third;
    sscanf(str, “%s %s %s”,first,second,third);

    program compiles then crashes

    or this

    char *str;
    char *first = “Learn”, *second = “C”, *third = “Online”;
    sprintf(str, “%s %s %s”,first,second,third);

    now if i get rid of the pointers used to store the individual words and assign a buffer(length of acceptable) it works fine.

    exp.
    char *str = “Learn C Online”;
    char first[10], second[10], third[10];
    sscanf(str, “%s %s %s”,first,second,third);
    printf(“%s %s %s”,first,second,third);

    any help would be great!
    also by using the pointers I am assuming that the buffers are not needed????

  10. Anonymous says:

    good

  11. Anonymous says:

    can anybody tell me why using * in declaring character such as char *a;

  12. Christopher says:

    @michael: char *str; … by itself it basically just a pointer to a character. When you later try to assign multiple characters, starting at *str, you can overwrite adjacent memory allocations, and that can cause a crash. If you reserve enough memory space during declaration (ie: char str[30]) you should be ok.

  13. vel says:

    how to get two number from user and then print the number with operator like 2*3 in one line

  14. suresh says:

    woww thank you so much……
    it helps me a lot for my first interview….
    once again thank you sooooo much……….

    suggestion: ” IF YOU PROVIDE WORK SPACE FOR THIS TUTORIAL IT WILL HANDY FOR THE BEGINNERS…..(PROVIDE ONLINE EDITOR)……THEN WE WILL LEARN THE SUBJECT AND PRACTICE THE SUBJECT IN ONE PLACE…
    THANK U SOOO MUCH

  15. alla says:

    really merci

  16. Jay says:

    good explanation

  1. March 2, 2014

    […] sscanf and sprintf […]

  2. March 2, 2014

    […] sscanf and sprintf […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet