第一种方法:
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc,char * argv[])
{
struct timeval tv;
while(1)
{
gettimeofday(&tv,NULL);
printf("time %u:%u\n",tv.tv_sec,tv.tv_usec);
sleep(2);
}
return 0;
}
第二种方法:(只能获取到秒数,不能更精确)
#include <time.h>
int main(int argc, char*argv[])
{
int time = time(0); //这里还不会超过int的表达范围,不会溢出
return 0;
}