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.
excellent.
sir,please provide a complete example programe with output.
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);
Hi while trying to run this it returns a error ‘ Segmentation fault’ please help
EXCELLENT!
Good Explaination…
add
str=(char *)malloc(strlen(first)+strlen(second)+strlen(third)+3);
before sprintf statement to remove segmentation fault error
excellent work…keep it up
excellent work…thankyou
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????
nice
good
can anybody tell me why using * in declaring character such as char *a;
@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.
how to get two number from user and then print the number with operator like 2*3 in one line
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
really merci
good explanation