博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言time函数怎么使用_如何在C / C ++中使用time()函数?
阅读量:2529 次
发布时间:2019-05-11

本文共 2660 字,大约阅读时间需要 8 分钟。

c语言time函数怎么使用

In this article, we’ll take a look at using the time() function in C/C++.

在本文中,我们将研究在C / C ++中使用time()函数。

The time() function is a useful utility function that we can use to measure the elapsed time of our program.

time()函数是一个有用的实用程序函数,可用于测量程序的经过时间。

Let’s look at how we can use this function, using some simple examples!

让我们来看一些简单的例子,看看如何使用此功能!



C / C ++中time()函数的基本语法 (Basic Syntax of the time() function in C/C++)

This function belongs to the header file <time.h>, so we must include this before calling time().

该函数属于头文件<time.h> ,因此在调用time()之前必须包含此文件。

#include 
time_t time(time_t *tloc);

This takes in a pointer to a time_t datatype and returns the time elapsed since 00:00:00 UTC, January 1, 1970.

这将获取一个指向time_t数据类型的指针,并返回从1970年1月1日UTC 00:00:00开始经过的时间。

The time value returned is in seconds, so you must make suitable conversions from it.

返回的时间值以秒为单位,因此您必须从中进行适当的转换。

If tloc is not a null pointer, the returned value is also stored in the object pointed to by second.

如果tloc 不是空指针,则返回的值也存储在second指向的对象中。

Otherwise, if tloc is NULL, the return value is not stored anywhere.

否则,如果tlocNULL ,则返回值不会存储在任何地方。

Let’s look at some simple examples now.

现在让我们看一些简单的例子。

在C / C ++中使用time()–一些示例 (Using time() in C/C++ – Some Examples)

Let’s first look at the case when tloc is a NULL pointer.

首先让我们看一下tloc是NULL指针的情况。

#include 
#include
int main() { time_t num_seconds = time(NULL); // Passing NULL to time() printf("Since 1st January 1970 UTC, No. of seconds = %d, No. of days = %d", (int)num_seconds, (int)num_seconds/86400); return 0;}

Output

输出量

Since 1st January 1970 UTC, No. of seconds = 1592317455, No. of days = 18429

As of the time of writing (June 16 2020), this is indeed true!

截至撰写本文时(2020年6月16日),确实如此!

Now, let’s go to the second case, when tloc is not NULL.

现在,让我们转到第二种情况,即tloc不为NULL时。

In this case, since the return value will be stored to the pointer location, there is no need to assign it separately!

在这种情况下,由于返回值将存储在指针位置,因此无需单独分配它!

#include 
#include
int main() { time_t tloc; time(&tloc); // Storing the return value to tloc printf("Since 1st January 1970 UTC, No. of seconds = %d, No. of days = %d", (int)tloc, (int)tloc/86400); return 0;}

This will give a similar output as before.

这将提供与以前相似的输出。



结论 (Conclusion)

In this article, we learned about using the time() function in C / C++, to get the time elapsed since the first epoch.

在本文中,我们学习了如何在C / C ++中使用time()函数来获取自第一个时期以来所经过的时间。

For more content on C and C++, do go through our on C programming!

有关C和C ++的更多内容,请阅读我们有关C编程的 !

参考资料 (References)

  • on time() in C

    on time()in C


翻译自:

c语言time函数怎么使用

转载地址:http://joozd.baihongyu.com/

你可能感兴趣的文章
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>