在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語言項目中進行時間相關的操作。