被叫对象'时间'不是一个功能

Kha*_*Ali 1 c time stdio

我使用一个非常简单的一行代码来获取ubuntu上C的当前时间,但它给了我以下错误: Called object 'time' is not a function

代码是: int currentTime = (unsigned int)time(NULL);

这个错误完全没有意义,可能的原因是什么?

pax*_*blo 6

无法看到您的实际代码使其变得困难,但首先,您需要确保已包含 time.h以便声明该函数.

其次,你需要确保你的代码中没有其他东西被调用time.

例如,此代码工作正常:

#include <stdio.h>
#include <time.h>

int main(){
    int currentTime = (unsigned int)time(NULL);
    printf("%u\n", currentTime);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码:

#include <stdio.h>
int time;

int main(){
    int currentTime = (unsigned int)time(NULL);
    printf("%u\n", currentTime);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

生产:

testprog.c: In function ‘main’:
testprog.c:5:38: error: called object ‘time’ is not a function
Run Code Online (Sandbox Code Playgroud)

在我的Debian盒子上,就像 Ubuntu 一样,只有更好:-)