In the code below I was told that if I wanted to ensure that a single digit day in a date was formatted as say....09, rather than 9, ex. 9/2/09 instead of 9/2/9, that I should change my placeholder from %d to %02d. That worked, however only for the day. I changed the other %d to %02d in order to preserve a two digit format, such as 05 instead of 5. But the year and month part of the mm/dd/yy did not change, even with %02d, they still printed as a single digit. This isn't a problem of any kind, other than I'm curious to know why my below code didn't do the same thing for each number.
Thanks
Scott
#include <stdio.h>
main ( )
{
int month ;
int day ;
int year ;
printf ("Enter a date (mm/dd/yy):") ;
scanf ("%d/%d/%d", &month, &day, &year) ;
printf ("You entered the date: %02d/%02d/%02d\n", year, month, day) ;
return 0 ;
Enter a date (mm/dd/yy):02/09/09
You entered the date: 9/2/09
Scott-Deans-MacBook-Pro:documents scottdean$
Thanks
Scott
#include <stdio.h>
main ( )
{
int month ;
int day ;
int year ;
printf ("Enter a date (mm/dd/yy):") ;
scanf ("%d/%d/%d", &month, &day, &year) ;
printf ("You entered the date: %02d/%02d/%02d\n", year, month, day) ;
return 0 ;
Enter a date (mm/dd/yy):02/09/09
You entered the date: 9/2/09
Scott-Deans-MacBook-Pro:documents scottdean$