473,786 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

May output the char one after one every other second?

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

Feb 16 '07 #1
16 3550
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.

Feb 16 '07 #2
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <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
Feb 16 '07 #3
Richard Bos wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <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.

Feb 16 '07 #4
On 2ÔÂ16ÈÕ, ÏÂÎç5ʱ22·Ö, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <true...@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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

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

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

Feb 16 '07 #5
Harald van Dijk wrote:
Richard Bos wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <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.
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.

Feb 16 '07 #6
liking C lang. wrote:
On 2ÔÂ16ÈÕ, ÏÂÎç5ʱ22·Ö, r...@hoekstra-uitgeverij..nl (Richard Bos) wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <true...@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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

as the FAQ mentions,use a do-nothing loop like
for(i=0;i<10000 000;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.

Feb 16 '07 #7
"liking C lang." <20*******@gmai l.comwrote:
On 2=D4=C216=C8=D5 , =CF=C2=CE=E75=C A=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<100 00000;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
Feb 16 '07 #8
"liking C lang." <20*******@gmai l.comschrieb im Newsbeitrag
news:11******** **************@ a75g2000cwd.goo glegroups.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
Feb 16 '07 #9
On 2ÔÂ16ÈÕ, ÏÂÎç9ʱ49·Ö, "Joachim Schmitz" <nospam.schm... @hp.comwrote:
"liking C lang." <2008mu...@gmai l.comschrieb im Newsbeitragnews :11************ **********@a75g 2000cwd.googleg roups.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.

Feb 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3980
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
4
6749
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if it caught anything, it's very picky... Anyway, the code below works on Dev, and it compiles fine on Borland, but when I run it from borland, there is no output, no error, it just skips right over the freind ostream call. HELP! I'm new to this...
24
2704
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
24
2018
by: tizi_de | last post by:
Hello all, I'm looking for a sample program in C to print out lines not to the standard MS Dos Box but into a different control e.g. text control. Has C the possibility to do printouts to a control instead of using the MS Dos Box? And what control can I use for this purpose when I need to have the most similar look to the MS Dos Box? Every help is greatly appreciated.
18
1955
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
19
33438
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
4
4658
by: keith | last post by:
I've been beating my head against this for a little while, so perhaps someone can help me out here? The code below should output exactly as follows (the hex data lines up in a fixed font): DATA 0123456789abcdef0123456789abcdef 0123456789abcdef0123456789abcdef and the code does indeed do that when I use the fprintf() call. When I comment that line out and use the iostream line, I get gibberish. I guess I'm just not 'getting' C++...
13
1993
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content from a file then arrange the data as a single string. */ #include <stdio.h> char* returnArrayFromFile(char* file_name) { // Try opening a file
1
2673
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.