It reads data from a character string.
sscanf(string,formatspecifier,&var1,&var2,……..)
String refers to the string of characters to be read from.
Format string is a character string containing some required formatting information.
Var1, var2, etc. represent each input data item.
For example, sscanf(string,"%d%d",&hours,&minutes);
This function is used Write data into a character string.
sprintf(string,format specifier,&var1,&var2…….);
String refers to the string of characters to be written.
A format specifier is a character string that contains some required formatting information.
Var1, var2, etc. represent each input data item.
Example - sprint(value, "cube of two is %d and square of two is %d
", 2*2*2, 2*2);
//value=cube of two is 8 and square of two is 4.
Live Demo
#include<stdio.h> int main(){ char instring[]="Tutorials Point"; char outstring[50],string1[10],string2[10]; sscanf(instring,"%s %s",string1,string2); printf("%s</p><p>",string1); printf("%s",instring); return 0; }
Tutorials Tutorials Point
Live demonstration
#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; }
adding two numbers 20 and 30 the result is 50
The above is the detailed content of What is the purpose of sprintf() and sscanf() functions in C language?. For more information, please follow other related articles on the PHP Chinese website!