The sscanf()
function allows us to read formatted data from a string rather than standard input through the keyboard.
int sscanf( const char* buffer, const char* format, ... );
The sscanf()
function reads data from the buffer and stores the values into their respective variables.
#include<stdio.h>#include<string.h>int main(){char *str = "Behzad Ahmad 21";char firstName[10], lastName[10];int age, temp;temp = sscanf(str, "%s %s %d", firstName, lastName, &age);printf("First Name: %s\n", firstName);printf("Last Name: %s\n", lastName);printf("Age: %d\n", age);return 0;}
scanf()
reads from the standard input stream stdin
. In comparison,
sscanf()
reads from the character string s
.
Both functions read characters, interpret them according to a format, and store the results in their arguments.