写一个时间函数,这个函数 有return 返回值 返回的是时分秒,主函数调用时间函数显示时间
时间: 2024-03-22 07:41:24 浏览: 24
好的,下面是一个示例函数,它返回当前时间的时、分、秒:
```c
#include <stdio.h>
#include <time.h>
char* get_current_time() {
time_t current_time;
struct tm * time_info;
static char time_string[9];
time(¤t_time);
time_info = localtime(¤t_time);
strftime(time_string, sizeof(time_string), "%H:%M:%S", time_info);
return time_string;
}
int main() {
char *time_str = get_current_time();
printf("The current time is %s\n", time_str);
return 0;
}
```
在这个示例中,我们定义了一个名为 `get_current_time` 的函数,它返回当前时间的时、分、秒。我们使用 `localtime` 函数获取当前时间,并将其存储在 `struct tm` 类型的变量中。然后,我们使用 `strftime` 函数将时间格式化为字符串,并将其存储在静态的字符数组中。最后,我们返回这个字符串指针。
在主函数中,我们调用 `get_current_time` 函数,将返回的字符串指针存储在一个变量中。然后,我们使用 printf 函数将返回的时间值输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)