Therefore, I need to specifically use struct tm to print my birthday, which I successfully completed. However, I also need to use strftime () to print in different formats. That I ran into my problem since 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));
strftime(buffer,15,"%d/%m/%Y",time_bday);
fprintf(stdout,"My birthday in D/M/Y format is %s",buffer);
}
return 0;
}
Errors:
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 tell me how to fix this?
EDIT: changing the_b_b time to str_bday works! But now the program displays a random time and date every time I run it.
EDIT: instead of fprintf () after strftime (), I used puts (buffer) and it worked fine. Also, changing buffer [15] to buffer [30], since I have hours, minutes, and seconds.
source
share