可以每隔一秒一个接一个地输出char吗? [英] May output the char one after one every other second?

查看:50
本文介绍了可以每隔一秒一个接一个地输出char吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个C编程,它可以每隔一秒后输出一个char $




例如:char a [50] =你好世界!输出h,下一秒输出

e,下一秒输出l ...

解决方案

喜欢C lang 。写道:


如何编写一个C编程,它可以在

之后每隔一秒输出一个char?


例如:char a [50] =" hello world!"输出h,下一秒输出

e,下一秒输出l ...



我假设你知道怎么写一个循环打印字符串

一次一个字符。如果你这样做,你唯一需要添加的是每次迭代等待一秒的
代码。在< time.h>中查看函数

。特别是,看一下标准库

的功能时钟。在伪代码中,你可以写


now = clock();

while(clock() - 现在< 1秒)

{/ *什么都不做* /}

您应该可以调整它以使其有效C.


" = UTF-8乙SGFyYWxkIHZhbiBExLNr = QUOT????; < tr ***** @ gmail.comwrote:


喜欢C lang。写道:


如何写一个C编程,它可能会每隔一秒后输出一个char $




我将假设你知道如何编写一个循环来打印字符串

一次一个字符。如果你这样做,你唯一需要添加的是每次迭代等待一秒的
代码。在< time.h>中查看函数

。特别是,看一下标准库

的功能时钟。在伪代码中,你可以写


now = clock();

while(clock() - 现在< 1秒)

{/ *什么都不做* /}

您应该可以调整它以使其有效C.



你可以,但你错了。 clock()测量处理器使用的时间,而不是实时的b $ b。为此,你需要time()和difftime()。


但即便如此,在一般情况下你最好不要使用这个

忙循环的等待方法。在多线程系统上,它将对您的计算机性能产生负面影响,而对于多用户
系统,它会非常反对 - 社会。你想做的是(像往常一样)

阅读FAQ:< http://c-faq.com/osdep/subsecond.html>


Richard


Richard Bos写道:


" =?utf-8?B?SGFyYWxkIHZhbiBExLNr? = QUOT; < tr ***** @ gmail.comwrote:


喜欢C lang。写道:


如何写一个C编程,它可能会每隔一秒后输出一个char $




我将假设您知道如何编写一个循环来打印字符串

一次一个字符。如果你这样做,你唯一需要添加的是每次迭代等待一秒的
代码。在< time.h>中查看函数

。特别是,看一下标准库

的功能时钟。在伪代码中,你可以写


now = clock();

while(clock() - 现在< 1秒)

{/ *什么都不做* /}

您应该可以调整它以使其有效C.



你可以,但你错了。 clock()测量处理器使用的时间,而不是实时的b $ b。为此,您需要time()和difftime()。



啊,对,谢谢你。


但即便如此,在一般情况下你'最好不要使用这个

忙循环等待方法。在多线程系统上,它将对您的计算机性能产生负面影响,而对于多用户
系统,它会非常反对 - 社会。你想做的是(像往常一样)

阅读常见问题解答:< http://c-faq.com/osdep/subsecond.html>



正如常见问题所述,标准C中无法获得亚秒级的b
分辨率。然而,OP想要等待一秒钟。这是标准C中可能的
(最好的计算机能力)。不幸的是,b $ b不幸的是,这样做的唯一标准方法是在多用户系统上进行反社交,但如果OP有兴趣学习C,那么这个

的情况下它可能是比其他替代品更好的解决方案,并且

可能没有其他用户担心。


How to write a C programming that it may output the char one after
one every other second?

For example: char a[50]="hello world!" output h,next second output
e,next second output l...

解决方案

liking C lang. wrote:

How to write a C programming that it may output the char one after
one every other second?

For example: char a[50]="hello world!" output h,next second output
e,next second output l...

I''m going to assume you know how to write a loop to print the string
one character at a time. If you do, the only thing you need to add is
code to wait a second on each iteration. Take a look at the functions
in <time.h>. In particular, take a look at the standard library
function clock. In pseudo-code, you could write

now = clock();
while (clock() - now < 1 second)
{ /* do nothing */ }

You should be able to adjust this to make it valid C.


"=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <tr*****@gmail.comwrote:

liking C lang. wrote:

How to write a C programming that it may output the char one after
one every other second?


I''m going to assume you know how to write a loop to print the string
one character at a time. If you do, the only thing you need to add is
code to wait a second on each iteration. Take a look at the functions
in <time.h>. In particular, take a look at the standard library
function clock. In pseudo-code, you could write

now = clock();
while (clock() - now < 1 second)
{ /* do nothing */ }

You should be able to adjust this to make it valid C.

You could, but you''d be wrong. clock() measures processor time used, not
real time. For that, you want time() and difftime().

But even then, in the general case you''re better off not using this
busy-loop method of waiting. On a multi-threaded system, it will have a
negative impact on the performance of your computer, and on a multi-user
system, it''s extremely anti-social. What you want to do is (as usual)
read the FAQ: <http://c-faq.com/osdep/subsecond.html>

Richard


Richard Bos wrote:

"=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <tr*****@gmail.comwrote:

liking C lang. wrote:

How to write a C programming that it may output the char one after
one every other second?

I''m going to assume you know how to write a loop to print the string
one character at a time. If you do, the only thing you need to add is
code to wait a second on each iteration. Take a look at the functions
in <time.h>. In particular, take a look at the standard library
function clock. In pseudo-code, you could write

now = clock();
while (clock() - now < 1 second)
{ /* do nothing */ }

You should be able to adjust this to make it valid C.


You could, but you''d be wrong. clock() measures processor time used, not
real time. For that, you want time() and difftime().

Ah, right, thank you for that.

But even then, in the general case you''re better off not using this
busy-loop method of waiting. On a multi-threaded system, it will have a
negative impact on the performance of your computer, and on a multi-user
system, it''s extremely anti-social. What you want to do is (as usual)
read the FAQ: <http://c-faq.com/osdep/subsecond.html>

As the FAQ mentions, there is no way in standard C for sub-second
resolution. However, the OP wanted to wait exactly one second. That is
possible in standard C (to the best of the computer''s ability). It''s
unfortunate that the only standard way of doing this is anti-social on
multi-user systems, but if the OP is interested in learning C, in this
case it may be a better solution than other alternatives, and there
may be no other users to worry about yet.


这篇关于可以每隔一秒一个接一个地输出char吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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