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
16 3552
On 2月16日, 下午4时33分, "liking C lang." <2008mu....@gma il.comwrote:
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?
santosh wrote:
Harald van D某k 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 havea
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.
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.
liking C lang. wrote:
On 2鏈16鏃, 涓嬪崍4鏃33鍒 , "liking C lang." <2008mu...@gmai l.comwrote:
>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.
liking C lang. wrote:
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.
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.
"Harald van D某k" <tr*****@gmail. comwrites:
[...]
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).
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
"Harald van D某k" <tr*****@gmail. comwrites:
[...]
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.
Yes, I got that wrong with clock, but after that, Richard Bos pointed
out time and difftime, which do check real time.
"Harald van D某k" <tr*****@gmail. comwrites:
Keith Thompson wrote:
>"Harald van D某k" <tr*****@gmail. comwrites: [...]
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.
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.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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);
|
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.
|
by: rajpal_jatin |
last post by:
int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
| |
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:
|
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++...
|
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
|
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
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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...
|
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,...
|
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梡lanning, coding, testing, and deployment梬ithout 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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |