I'm reading a book about C programming, at the end of each chapter it has some training exercise and one of them was to make a program that tells if a date is valid or not, the code below is what I did and I would like to know if it can be improved?
#include <stdio.h>
int main(){
int dd,mm,yy;
printf("write a date(day/month/year):");
scanf("%d/%d/%d",&dd,&mm,&yy);
int bissextile=(yy%4==0)?1:0;
if (dd>31 || dd<1 || mm>12 || mm<1)
printf("Invalid date.\n");
else
if((mm==2 && dd<30 && bissextile) || (mm==2 && dd<29 && bissextile==0))
printf("valid date.\n");
else
if((mm==4 || mm==6 || mm==9 || mm==11)&& dd<31)
printf("valid date.\n");
else
if(dd<31 && mm!=2)
printf("valid date.\n");
else
printf("Invalid date.\n");
}
mktime()
does not validate dates. It readily accepts values outside the primary range. "...the original values of the other components are not restricted to the ranges indicated above ..." \$\endgroup\$