#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static struct tm createDate(unsigned day, unsigned mon, int year) {
struct tm b = {0,0,0,day,mon-1,year-1900}; return b;
}
static int dateExceeded(unsigned day, unsigned mon, int year) {
struct tm b = createDate(day,mon,year);
time_t y = mktime(&b), now;
time(&now); // error C2143: syntax error : missing ';' before 'type'
double diff = difftime(y, now) / (60 * 60 * 24); // error C2065: 'diff' : undeclared identifier
return (diff < 0);
}
static void randomEvent(){
srand(time(NULL));
if ( rand()%10) {
printf("Do something here\n"); // C2143: syntax error : missing ';' before 'type'
}
}
最佳答案
如果将其编译为 C89 代码,则应在 block 的开头声明变量。您不能在 block 的中间声明 double diff
:
static int dateExceeded(unsigned day, unsigned mon, int year) {
double diff;
struct tm b = createDate(day,mon,year);
time_t y = mktime(&b), now;
time(&now);
diff = difftime(y, now) / (60 * 60 * 24);
return (diff < 0);
}
关于谁能看出这有什么问题(C 中与时间相关的函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1251288/