Recall how with printf(), we could control the minimum length of the output string. In a similar way, we can control the maximum length of the input string for conversion with a specifier. For example, %3d will accept no more than three digits for conversion into an integer. The following program reads a group of digits, each of which is intended to represent a date—the first four digits for the year, the next two for the month, and the last two for the day:
#include <stdio.h>
int main( void ) {
int year , month , day;
int numScanned;
while( printf("Enter mmddyyyy (any other character to quit): "),
numScanned = scanf( "%2d%2d%4d" , &month , &day , &year ) ,
numScanned > 0 )
printf( "%d/%d/%d\n" , month , day , year );
printf( "\nDone\n" );
}
After declaring variables to hold the input values, a while()… loop is used to repeatedly accept eight digits to...