在C语言项目中,可以使用time()函数来获取当前时间的秒数,从而实现时间相关的操作。以下是一些有效地使用time()函数的方法:
time_t current_time;
current_time = time(NULL);
printf("Current time: %ld\n", current_time);
time_t current_time;
struct tm *time_info;
current_time = time(NULL);
time_info = localtime(¤t_time);
printf("Current time: %d-%d-%d %d:%d:%d\n", time_info->tm_year + 1900, time_info->tm_mon + 1,
time_info->tm_mday, time_info->tm_hour, time_info->tm_min, time_info->tm_sec);
clock_t start_time, end_time;
double exec_time;
start_time = clock();
// 执行一些操作
end_time = clock();
exec_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Execution time: %f seconds\n", exec_time);
srand(time(NULL));
int random_num = rand();
printf("Random number: %d\n", random_num);
通过以上方法,可以有效地使用time()函数在C语言项目中进行时间相关的操作。