Harald van D某k wrote:
> Richard Bos wrote:
> > "=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <> wrote:
> >
> > > 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.
I don't think it's possible, in the sense that it is guaranteed,
within standard C, to pause any length of time. You can get the
current time with time(), but in a multitasking system, the OS might
decide to preempt your program such that, you overshoot the
requirements. clock(), of course, measures processor time, which is
different. Realtime behaviour is not guaranteed under preemptively
multitasking systems. The best you can get is to elevate your priority
and set a alarm or timer. All that is of course beyond the scope of
this group.
|