473,765 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TIP: Using a delay in C

Using a delay in C, using Linux. gcc-3.3
This program do a boy in a bike runing in the screen. :D
Its cool to see about games and delay in C.

#include<stdio. h>
int branco(int j){
int k;
for (k=0;k<j;k++)
{
printf(" ");
}

}
int main(){
int i;
for(i=0;i<20;i+ +)
{
//limpa a tela
printf("\033[H\033[2J");
usleep(50000);
branco(i);
printf(" __@\n");
branco(i);
printf(" _`\\<,_\n");
branco(i);
printf(" (*)/ (*)\n");

}
}
Nov 14 '05 #1
17 31933
"silveira neto" <ot**********@y ahoo.com.br> wrote in message
news:be******** *************** *******@localho st.talkaboutpro gramming.com...
Using a delay in C, using Linux. gcc-3.3
This program do a boy in a bike runing in the screen. :D
Its cool to see about games and delay in C.
What you have below is not (standard) C.

#include<stdio. h>
int branco(int j){
int k;
for (k=0;k<j;k++)
{
printf(" ");
}

}
int main(){
int i;
for(i=0;i<20;i+ +)
{
//limpa a tela
printf("\033[H\033[2J");
This string argument to 'printf()' depends upon the standard
input device interpreting it a particular way. Not portable at all.
usleep(50000);
No such function in standard C.
branco(i);
printf(" __@\n");
branco(i);
printf(" _`\\<,_\n");
branco(i);
printf(" (*)/ (*)\n");

}
return 0; /* mandatory for C89, optional for C99 */
}


A 'cute' little program only usable on a limited number
of platforms/configurations.

-Mike
Nov 14 '05 #2
> expand delay.c
#include<stdio. h>
#include<stdlib .h>

void branco(int j) {
for (int k = 0; k < j; ++k) {
printf(" ");
}
}

int main(int argc, char* argv[]) {
extern int usleep(unsigned long usec);
for(int i = 0; i < 20; ++i) {
//limpa a tela
printf("\033[H\033[2J");
usleep(50000);
branco(i);
printf(" __@\n");
branco(i);
printf(" _`\\<,_\n");
branco(i);
printf(" (*)/ (*)\n");
}
return EXIT_SUCCESS;
}

Nov 14 '05 #3
"silveira neto" <ot**********@y ahoo.com.br> wrote in message
news:be******** *************** *******@localho st.talkaboutpro gramming.com...
Using a delay in C, using Linux. gcc-3.3
This group is about ISO C, i.e. programs which compile and run on _all_
platforms, not just Linux using GNU C.
This program do a boy in a bike runing in the screen. :D
Its cool to see about games and delay in C.
Please post such unix specific 'tips' to unix specific groups.
<snip>


A near ISO C equivalent would be something like...

#include <stdio.h>
#include <time.h>

void usleep(unsigned long u)
{
clock_t end, start = clock();
if (start == (clock_t) -1) return;
end = start + CLOCKS_PER_SEC * (u / 1000000.0);
while (clock() != end) ;
}

int main(void)
{
int i;
for (i = 0; i < 20; i++)
{
usleep(500000);
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n\n\n\n\n\n");
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n\n\n\n\n\n");
printf("%*s\n", 10 + i * 2, " __@ ");
printf("%*s\n", 10 + i * 2, " _`\\<,_");
printf("%*s\n", 10 + i * 2, "(*)/ (*) ");
}
return 0;
}

--
Peter
Nov 14 '05 #4
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:u%******** **********@news read2.news.pas. earthlink.net.. .
"silveira neto" <ot**********@y ahoo.com.br> wrote in message
news:be******** *************** *******@localho st.talkaboutpro gramming.com...

int main(){

....
return 0; /* mandatory for C89, optional for C99 */
}


It's not _mandatory_ in C89 in the strictest sense, merely desirable.

--
Peter
Nov 14 '05 #5
On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
<ai***@acay.com .au> wrote:
while (clock() != end) ;


You may be spinning for a long time. I would recommend < instead of
!=.

- Sev

Nov 14 '05 #6
Peter Nilsson wrote:
silveira neto wrote:
Using a delay in C, using Linux. gcc-3.3


This group is about ISO C,
i.e. programs which compile and run on _all_ platforms,
not just Linux using GNU C.


Nonsense!
There are almost *no* useful C programs
which compile and run on _all_ platforms.
This program do a boy in a bike running in the screen. :D
It's cool to see about games and delay in C.


A near ISO C equivalent would be something like...

#include <stdio.h>
#include <time.h>

void usleep(unsigned long u) {
clock_t start = clock();
if (start != (clock_t)(-1)) {
clock_t end = start + CLOCKS_PER_SEC* (u/1000000.0);
while (clock() != end);
}
}
int main(int argc, char* argv[]) {
for (int i = 0; i < 20; ++i) {
usleep(500000);
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n\n\n\n\n\n");
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n\n\n\n\n\n");
printf("%*s\n", 10 + i * 2, " __@ ");
printf("%*s\n", 10 + i * 2, " _`\\<,_");
printf("%*s\n", 10 + i * 2, "(*)/ (*) ");
}
return 0;
}


It's more portable. And even more readable.
But it sucks compared to the original when I run it.

Nov 14 '05 #7
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:40******** ******@jpl.nasa .gov...
Peter Nilsson wrote:
silveira neto wrote:
Using a delay in C, using Linux. gcc-3.3
This group is about ISO C,
i.e. programs which compile and run on _all_ platforms,
not just Linux using GNU C.


Nonsense!
There are almost *no* useful C programs
which compile and run on _all_ platforms.


On the contrary, there's quite a lot you can do in pure ISO C. [cf. early
POVRAY]

But yes, I should have said code, not programs.
<snip ISO variation>

It's more portable. And even more readable.
But it sucks compared to the original when I run it.


Maybe next time, I'll allocate the time to output a DVD quality mpeg file.

--
Peter
Nov 14 '05 #8
"Severian" <se******@chlam ydia-is-not-a-flower.com> wrote in message
news:8i******** *************** *********@4ax.c om...
On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
<ai***@acay.com .au> wrote:
while (clock() != end) ;


You may be spinning for a long time. I would recommend < instead of
!=.


True! My port of gcc has CLOCKS_PER_SEC of 93 and a resolution of 5!

--
Peter
Nov 14 '05 #9
In 'comp.lang.c', "Peter Nilsson" <ai***@acay.com .au> wrote:
> int main(){

...
return 0; /* mandatory for C89, optional for C99 */
> }


It's not _mandatory_ in C89 in the strictest sense, merely desirable.


I think it is (mandatory), unless, the returned value is not defined (some
platform force 0, but it's not a C90 rule).

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #10

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

Similar topics

14
18832
by: Des L. Davis | last post by:
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003 Server Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000 Server If you run the code below, you'll notice something odd occuring. The MilliSecond value does not change after a 1Millisecond delay. Is this a bug or am I doing something wrong? Any assistance will be greatly appreciated
6
2585
by: Salty Dog | last post by:
Code: <%Response.Buffer = False%> <HTML> <HEAD> <title>Printing</title> </HEAD> <BODY> <center> <% FilePath = "c:\WUTemp\441817810_receiver.txt"
7
2943
by: mfeingold | last post by:
I am working on a system, which among other things includes a server and a ..net control sitting in an html page and connected to the server. I ran into a couple of problems, you guys might have some insight about. 1) It takes 20 sec or so to open a tcp socket from the client to the server. It just sits in the TcpClient.conect waiting for something. When I run the same control from a windows application it connects right away and works...
10
93993
by: AmitTrehan | last post by:
hi friends, I want to know how can we insert delay in vb.net like..... i want to show two strings on same label first one string then break of 5 secs and then second string how can i acheive this? i mean label should show first label.text("before") then it should wait for 5 secs and then it should show label.text("After")
0
1384
by: mookie | last post by:
m looking to create something similar to #region and #endregion using treeviews the problem is that instead of #region, i am using ;fold and ;endfold i am also allowed ot have a fold within a fold and that is where i am stuck. Private Sub rtb_TextChanged(ByVal sender As System.Object, ByVal e As
14
3286
by: Steve Teeples | last post by:
I don't understand why I cannot use a property to modify data within a struct. Can someone tell me why I get the error "Cannot modify the return value of "myData.TheData" because it is not a variable. Here is what breaks. struct Data { public Data(){} private int someData;
8
4234
by: Craig Williamson | last post by:
I'm trying to write a time delay loop in Borland C 3.1 (for legacy code) which has an accuracy of about 0.1ms. This change is due to a hardware change and the software is now running too fast to take proper readings. I have been using delay(1) in the past but it does not have the accuracy that I need for this situation. Does anyone know of a useful C function which can be used to create the time delay? If anyone could help that would...
3
2901
by: TamaThps | last post by:
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all being in the int main(). I am using visual studio on windows XP. In my code I've defined a local variable in a function but when I run the program and it gets to that function it says I have an undeclared variable. The code for the program...
7
5015
by: coolnags | last post by:
Hi, As we know IE SELECTdrop down list has limitations on tool tip on individual option items and also horizontal scroll bar. Can any one please tell any other alternative to show the tool tip using javascript or has any one implemented drop down list using div which has all the functionalites of drop down list. Thanks, Nags
0
9398
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,...
0
10160
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
10007
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
9951
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
9832
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...
1
7378
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...
0
6649
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();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2805
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.