任何人都可以看到这有什么问题(C中的时间相关功能)

0 c time srand

#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'
  } 
}
Run Code Online (Sandbox Code Playgroud)

Meh*_*ari 5

如果要将其编译为C89代码,则应在块的开头声明变量.您不能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); 
}
Run Code Online (Sandbox Code Playgroud)