Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > May output the char one after one every other second?

Reply
Thread Tools

May output the char one after one every other second?

 
 
liking C lang.
Guest
Posts: n/a
 
      02-16-2007
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...

 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-16-2007
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.

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      02-16-2007
"=?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().

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
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-16-2007
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.

 
Reply With Quote
 
liking C lang.
Guest
Posts: n/a
 
      02-16-2007
On 2月16日, 下午5时22分, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> "=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <true...@gmail.com> 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().
>
> 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-16-2007
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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-16-2007
liking C lang. wrote:
> On 2月16日, 下午5时22分, r...@hoekstra-uitgeverij..nl (Richard Bos) wrote:
> > "=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <true...@gmail.com> 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().
> >
> > 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.


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.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-16-2007
"liking C lang." <> wrote:

> On 2=D4=C216=C8=D5, =CF=C2=CE=E75=CA=B122=B7=D6, r...@hoekstra-uitgeverij.n=
> l (Richard Bos) wrote:
> > 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- =D2=FE=B2=D8=B1=BB=D2=FD=D3=C3=CE=C4=D7=D6 -
> >
> > - =CF=D4=CA=BE=D2=FD=D3=C3=B5=C4=CE=C4=D7=D6 -


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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-16-2007
"liking C lang." <> schrieb im Newsbeitrag
news: oups.com...
> 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


 
Reply With Quote
 
liking C lang.
Guest
Posts: n/a
 
      02-16-2007
On 2月16日, 下午9时49分, "Joachim Schmitz" <nospam.schm...@hp.com> wrote:
> "liking C lang." <2008mu...@gmail.com> schrieb im Newsbeitragnews:1171614832.759778.132930@a75g2000c wd.googlegroups.com...> 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Compare every bit of char versus unsigned char jamx C Programming 11 03-12-2006 08:06 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57