Monday, 3 June 2019

Skip the String or Char or int using format specifier %*

Format specifiers in C

  •  It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf().
    •  EG: %c, %d, %f, etc.
    • printf(char *format, arg1, arg2, …)

printing format:


  • A minus(-) sign tells left alignment.
  • A number after % specifies the minimum field width to be printed if the characters are less than the size of width the remaining space is filled with space and if it is greater than it printed as it is without truncation.
  • A period( . ) symbol seperate field width with the precision.
  •  %%    Prints % character

Program:

/* Skip the String or Char or int using format specifier %*  by Velraj.K
 * Check : http://velrajcoding.blogspot.in
 */

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

int main () {
    int day = 0, year = 0, ret;
    char weekday[20] = {0}, month[20] = {0}, dtm[100]= {0}, time[128] = {0}, meridiem[8] = {0};

    /*
     * The abbreviations am and pm derive from Latin:
     *   AM = Ante meridiem: Before noon
     *   PM = Post meridiem: After noon
     */
    strcpy( dtm, "Saturday March 25 1989 v1Hour before 10 AM" );
    //sscanf( dtm, "%s %*s %d  %d", weekday, month, &day, &year );
    /*
     * %*s  -> Skip the string
     * %*c  -> Skip the character
     * %*d  -> Skip the interger
     */
    ret = sscanf(dtm, "%s %*s %d  %d %*c %s %*s %*d %s", weekday, &day, &year, time, meridiem);



    printf("ret:%d Value: Day:<%d> Year:<%d> Week:<%s> Time:%s Meridiem:%s\n", ret, day, year, weekday, time, meridiem);
    printf("Velraj %n \n", &ret);

    return(0);
} 

Output: 

  /sample$ ./a.out ret:5 Value: Day:25 Year:1989 Week:saturday Time:1Hour Meridiem:AM

No comments:

Post a Comment