定时C程序循环 [英] Timed C Program Loop

查看:83
本文介绍了定时C程序循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据用户输入(在C程序中)循环一定时间.

I need to loop for a certain amount of time based on user input ( in C Program ).

示例:用户说循环2分钟(= 120秒).

Example: User says loop for 2 Minutes ( = 120 seconds).

while(time <= 2 Minutes)
{
    do something
}

我将如何在C语言中执行此操作?谢谢您的帮助!

How would I go about doing this in C? Thanks for your help!!

推荐答案

如果需要在特定时间段内执行多次操作,则可以使用

If you need to perform some operation as many times as possible within the specific period of time you can use time():

time_t secs = 120; // 2 minutes (can be retrieved from user's input)

time_t startTime = time(NULL);
while (time(NULL) - startTime < secs)
{
    ...
}

这篇关于定时C程序循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆