May output the char one after one every other second?

L

liking C lang.

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...
 
G

Guest

liking said:
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.
 
R

Richard Bos

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
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
 
G

Guest

Richard said:
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.
 
L

liking C lang.

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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -


as the FAQ mentions,use a do-nothing loop like
for(i=0;i<10000000;i++)
;
Ah,my computer must be too tired to crash.But this method is also a
better way.
Thank you.
 
S

santosh

Harald said:
Ah, right, thank you for that.


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.
 
S

santosh

liking said:
as the FAQ mentions,use a do-nothing loop like
for(i=0;i<10000000;i++)
;
Ah,my computer must be too tired to crash.But this method is also a
better way.

No it isn't. It's system and load specific. If you really want to do
this in a robust manner, you'll have to look at whatever facilities
that are offered by your system. For a toy or demo program, the above
method or what Harald suggested are probably fine.
 
R

Richard Bos

No, I didn't post all that quoted-illegible crap. Don't do that.
as the FAQ mentions,use a do-nothing loop like

Obviously you haven't read that page for comprehension. The FAQ
specifically and strongly says _not_ to use a do-nothing loop like that.
for(i=3D0;i<10000000;i++)
;
Ah,my computer must be too tired to crash.But this method is also a
better way.

No, it is massively inferior to using your brain and a system-specific
method. It is even greatly inferior to using time(). About the only
thing it is not inferior to is asking the user to use a stopwatch and
press enter every second.

Richard
 
J

Joachim Schmitz

liking C lang. said:
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...
in POSIX there is sleep(int seconds); maybe that helps here

bye, Jojo
 
L

liking C lang.

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...

#include <dos.h >
#include <stdio.h >

int main(void)
{
int i;

for (i=1; i <5; i++)
{
printf( "Sleeping for %d seconds\n ", i);
sleep(i);
}
return 0;
}


Above information is that I got from internet.I think
sleep() is best for a delay in the case of outputing
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...

Am I right?
 
G

Guest

santosh said:
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.

That's the sort of thing I hoped would be covered by "to the best of
the computer's ability". Though admittedly, it's more "to the best of
the operating system's ability" in that case.
 
C

Clever Monkey

liking said:
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...

#include <dos.h >
#include <stdio.h >

int main(void)
{
int i;

for (i=1; i <5; i++)
{
printf( "Sleeping for %d seconds\n ", i);
sleep(i);
}
return 0;
}


Above information is that I got from internet.I think
sleep() is best for a delay in the case of outputing
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...

Am I right?
My reading of this is, "iterate four times, passing to sleep() first 1,
then 2, then 3, then 4". For a total of 10 units passed to the sleep()
function.

I'm guessing this is not what you want to do.

You want to iterate over each character in your char array, printing
each character, waiting 1 second between each iteration.

How you implement your "wait" depends on what you want, of course. Your
local OS implementation of some sort of sleep() function is one way.
There are somewhat portable implementations of such functions floating
around out there.
 
S

santosh

liking said:
liking C lang. said:
one every other second?
For example: char a[50]="hello world!" output h,next second output
e,next second output l...

in POSIX there is sleep(int seconds); maybe that helps here

bye, Jojo

I think this method is best answer for the OP.
Thank you, Jojo.

Err, aren't you the OP?

Please try to format your posts better. As such they're very hard to
read. Just use the term "using Google Groups", and search Google
Groups's archive of this group to find informative articles on posting
to Usenet.
 
K

Keith Thompson

Harald van Dijk said:
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.

There's nothing in the standard that says you can do anything even
with 1-second resolution.

The OP wants to delay for 1 second; I doubt that he's interested in 1
second of CPU time, as opposed to 1 second of real time.

This is one of those things that you can *almost* do in purely
standard C, but very badly, but that you can do easily and cleanly in
most implementations using system-specific extensions.

Assuming that the implementation provides a function called, lets say,
"sleep", that delays for a specified number of seconds, it would
useful for the OP's purposes. He should also call fflush(stdout)
after each character (otherwise the entire line is likely to be
buffered before being printed).
 
G

Guest

Keith said:
There's nothing in the standard that says you can do anything even
with 1-second resolution.

The OP wants to delay for 1 second; I doubt that he's interested in 1
second of CPU time, as opposed to 1 second of real time.

Yes, I got that wrong with clock, but after that, Richard Bos pointed
out time and difftime, which do check real time.
 
K

Keith Thompson

Harald van Dijk said:
Yes, I got that wrong with clock, but after that, Richard Bos pointed
out time and difftime, which do check real time.

Yes, but (a) there's no guarantee that the resolution of time() is 1
second or better (though I don't kow of any system where it isn't),
and (b) time() and difftime() do not provide decent ways to solve the
OP's problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top