Cheppy
Cheppy

Reputation: 45

Is it ok to ignore part of sscanf input string

Is it ok if we ignore part of input string to sscanf function.

In the below code , I am interested only in day and year, so I don't collect the weekday and month into variables.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   int day, year;
   char weekday[20], month[20], dtm[100];

   strcpy( dtm, "Saturday March 25 1989" );

   if(sscanf( dtm, "Saturday March %d  %d", &day, &year ) == 2)
       printf("%d, %d \n", day, year );
   else
       printf("error");
 
   return 0;
}

Upvotes: 0

Views: 450

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

Yes, it is fine.

Also you can ignore general strings by adding * to the format specifier like

if(sscanf( dtm, "%*s%*s%d%d", &day, &year ) == 2)

Upvotes: 3

Related Questions