Wednesday

sscanf and sprintf functions


 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.

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |