The sscanf() function
It reads data from a character string.
Syntax
sscanf(string,formatspecifier,&var1,&var2,……..)
String refers to the char string to read from.
Format string refers to char string containing certain required formatting information.
Var1,var2 etc., represent the individual input data items.
For example, sscanf(string,"%d%d",&hours,&minutes);
The sprintf() function
This function is used to write data to a character string.
Syntax
sprintf(string,format specifier,&var1,&var2…….);
String refers to char string to write.
Format specifier refers to char string containing certain required formatting information.
Var1,var2 etc., represent the individual input data items.
Example − sprint(value,"cube of two is %d and square of two is %d\n", 2*2*2 ,2*2);
//value=cube of two is 8 and square of two is 4.
Example for sscanf() function
#include<stdio.h> int main(){ char instring[]="Tutorials Point"; char outstring[50],string1[10],string2[10]; sscanf(instring,"%s %s",string1,string2); printf("%s\n",string1); printf("%s",instring); return 0; }
Output
Tutorials Tutorials Point
Example for sprintf() function
#include <stdio.h> int main(){ char value[50]; int p = 20, q = 30, r; r= p + q; sprintf(value, "adding two numbers %d and %d the result is %d", p, q,r); printf("%s", value); return 0; }
Output
adding two numbers 20 and 30 the result is 50