Header file:
stdio.h
Synopsis:
int sscanf(char *str, const char *format, ...);
Description:
Performs input format conversion. It is similar to scanf except that the inputs are obtained from the string "str".
sscanf function C example:
#include<stdio.h>
#include<string.h>
int main() {
char name[100], org[100], input[200];
int age;
printf("Enter your name, org name and age:");
fgets(input, 150, stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%s %s %d", name, org, &age);
printf("Name: %s\nOrganization:%s\n", name, org);
printf("Age: %d\n", age);
return 0;
}
#include<string.h>
int main() {
char name[100], org[100], input[200];
int age;
printf("Enter your name, org name and age:");
fgets(input, 150, stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%s %s %d", name, org, &age);
printf("Name: %s\nOrganization:%s\n", name, org);
printf("Age: %d\n", age);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Enter your name, org name and age:JP ProgrammingOnline 22
Name: JP
Organization: ProgrammingOnline
Age: 22
Enter your name, org name and age:JP ProgrammingOnline 22
Name: JP
Organization: ProgrammingOnline
Age: 22
No comments:
Post a Comment