C语言时间处理实例分享_C语言教程-查字典教程网
C语言时间处理实例分享
C语言时间处理实例分享
发布时间:2016-12-28 来源:查字典编辑
摘要:一、简介时间处理在编程中经常遇到,包括程序的运行时间和显示时间等。在标准C中,日期和时间的处理包含在time.h的头文件中,需要使用日期和时...

一、简介

时间处理在编程中经常遇到,包括程序的运行时间和显示时间等。在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h.

二、实例

1、计算时差

#include <stdio.h> #include <sys/time.h> #include <unistd.h> int main() { struct timeval start, end; unsigned long spend_time; gettimeofday( &start, NULL ); printf("start : %d.%dn", start.tv_sec, start.tv_usec); sleep(1); gettimeofday( &end, NULL ); printf("end : %d.%dn", end.tv_sec, end.tv_usec); //微秒时差 spend_time=1000000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec); printf("%ldn",spend_time); return 0; }

编译

gcc -g -o time_diff time_diff.c

运行

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C语言学习
    热门C语言学习
    编程开发子分类