This blog is under construction

Friday, 27 April 2012

sscanf example in C

Header file:

     int sscanf(char *str, const char *format, ...);

    Performs input format conversion. It is similar to scanf except that the inputs are obtained from the string "str".

sscanf function C example:

  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;

  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Enter your name, org name and age:JP ProgrammingOnline 22
  Name: JP
  Organization: ProgrammingOnline
  Age: 22

No comments:

Post a Comment