Instead of:
Code:
if(mon<10)
{
sprintf(d,"%d",mon);
date[0]='0';
date[1]=d[0];
}
else
sprintf(date,"%d",mon);
Just do:
Code:
sprintf(date, "%02d", mon);
In fact the whole thing could just be simplified to:
Code:
char *rdate = malloc(9);
int mon = randr(1, 12);
int year = randr(1995, 2015);
int day = randr(1, mon == 2 ? 28 : 31); // note that this doesn't account for 30-day months or leap years
sprintf(rdate, "%02d%02d%04d", mon, day, year);
return rdate;
A better way for the day might be:
Code:
int days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = randr(1, days_per_month[mon - 1]);
... which still doesn't account for leap years, but at least you won't end up with April 31st.