So I need to specifically use struct tm to print out my birthday, which I did successfully. However, I am also required to use strftime() to print it in different formats. That's where I encounter my problem, as strftime() only recognizes pointer parameters.
#include <stdio.h>
#include <time.h>
int main(){
struct tm str_bday;
time_t time_bday;
char buffer[15];
str_bday.tm_year = 1994 - 1900 ;
str_bday.tm_mon = 7 - 1;
str_bday.tm_mday = 30;
str_bday.tm_hour = 12;
str_bday.tm_min = 53;
time_bday = mktime(&str_bday);
if(time_bday == (time_t)-1)
fprintf(stdout,"error\n");
else
{
fprintf(stdout,"My birthday in second is: %ld \n",time_bday);
fprintf(stdout,"My birthday is: %s\n", ctime(&time_bday));//Wed July 22 12:53:00 1998
strftime(buffer,15,"%d/%m/%Y",time_bday);
fprintf(stdout,"My birthday in D/M/Y format is %s",buffer);
}
return 0;
}
The errors are:
Error: passing argument 4 of ‘strftime’ makes pointer from integer without a cast
expected ‘const struct tm * restrict’ but argument is of type ‘time_t’
Can someone please tell me how to fix it?
EDIT: Changing time_bday to &str_bday works! But now the program outputs random time and date every time I run it.
EDIT: Instead of fprintf() after strftime(), I used puts(buffer), and it worked perfectly. Also, changing buffer[15] to buffer[30] as I have hours, minutes and seconds.