gpt4 book ai didi

c - c中的时间函数不断返回-1

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:30 24 4
gpt4 key购买 nike

您好,我有一个小问题,因为函数 time() 一直返回 -1 有什么可行的方法可以解决这个问题吗?

我试过:

time_t start, end;
start = time(NULL);
//do something
end = time(NULL);

但是当我调试时,两者的值都是 -1。 我试过了:

time_t start, end;
time(&start);
// do something
time(&end);

但在这种情况下,两个值都保持为 0。

最佳答案

根据 ISO C,如果 time() 无法确定时间,则返回 -1 是完全有效的。来自 ISO C11 7.27.2.4 The time function/3(尽管这种行为已经存在很长时间了):

The time function returns the implementation’s best approximation to the current calendar time. The value (time_t)(-1) is returned if the calendar time is not available.

鉴于您正在使用 Simplicity Studio,这是一个经常用于嵌入式系统的软件包,您的目标环境可能无法建立日历时间(例如,如果它没有实时时钟查询)。

关于c - c中的时间函数不断返回-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33672101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com