473,480 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Create 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 31898
"silveira neto" <ot**********@yahoo.com.br> wrote in message
news:be******************************@localhost.ta lkaboutprogramming.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**********@yahoo.com.br> wrote in message
news:be******************************@localhost.ta lkaboutprogramming.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******@mkwahler.net> wrote in message
news:u%******************@newsread2.news.pas.earth link.net...
"silveira neto" <ot**********@yahoo.com.br> wrote in message
news:be******************************@localhost.ta lkaboutprogramming.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******@chlamydia-is-not-a-flower.com> wrote in message
news:8i********************************@4ax.com...
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**********@noos.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
First, thanks for the atention.
Sorry for my faults in the program, I learned C the day before.
And sorry for some words in portuguese, my live in Brazil, I forget translate in the source.
So, I think is important the portability of the source. I would know where I can learn about the C that I can write to all plataforms, if this is possible.
What C is this C that we are talking about?

I compiled the remaked program, thanks Peter Nilsson, but as Robert Tisdale, I dont liked the graphics. Maybe there is another way to do this program portable and cool. :)
Thanks.

Nov 14 '05 #11
First, thanks for the atention.
Sorry for my faults in the program, I learned C the day before.
And sorry for some words in portuguese, my live in Brazil, I forget translate in the source.
So, I think is important the portability of the source. I would know where I can learn about the C that I can write to all plataforms, if this is possible.
What C is this C that we are talking about?

I compiled the remaked program, thanks Peter Nilsson, but as Robert Tisdale, I dont liked the graphics. Maybe there is another way to do this program portable and cool. :)
Thanks.

Nov 14 '05 #12
First, thanks for the atention.
Sorry for my faults in the program, I learned C the day before.
And sorry for some words in portuguese, my live in Brazil, I forget translate in the source.
So, I think is important the portability of the source. I would know where I can learn about the C that I can write to all plataforms, if this is possible.
What C is this C that we are talking about?

I compiled the remaked program, thanks Peter Nilsson, but as Robert Tisdale, I dont liked the graphics. Maybe there is another way to do this program portable and cool. :)
Thanks.

Nov 14 '05 #13
ehr, sorry for my tripple post! :P
nothing happend, so I pushed f5 sometimes.

Nov 14 '05 #14
"silveira neto" <ot**********@yahoo.com.br> wrote in message
news:93******************************@localhost.ta lkaboutprogramming.com...
First, thanks for the atention.
Sorry for my faults in the program, I learned C the day before.
IMO nobody can learn C in a day. I've been using C for
about 16 years, but I would not claim to be an 'expert' with it.
I'd only go so far to say I'm more or less 'comfortable' or
'conversant' with it (but not with many of it's 'dark corners'
-- i.e. obscure, less often used features).
And sorry for some words in portuguese, my live in Brazil, I forget translate in the source.

Not a problem.
So, I think is important the portability of the source.
It can be important in many cases, yes.
I would know where I can learn about the C that I can write to all
plataforms, if this is possible.
It is possible. Learn how from good books.
See www.accu.org for peer reviews by the experts.
What C is this C that we are talking about?
The language which is the topic of comp.lang.c is the standard
C language as defined by the international standard ISO/IEC 9899.


I compiled the remaked program, thanks Peter Nilsson, but as Robert

Tisdale, I dont liked the graphics. Maybe there is another way to do this
program portable and cool. :)

Standard C does not support graphics. But graphics can indeed be
done by applying a specialized library to a C program. However,
this will necessarily limit its portability (to those platforms
where the special library has been implemented). One example of
such a 'multi-platform-capable' library is 'wxWindows'.
Nonportable code isn't 'bad' per se, sometimes it's the only way
to do what you need. But I (and many others) still recommend that
as much of a C program as possible be written portably, and that
the platform-specific portions be clearly identified and separated
into their own modules. This will make porting less cumbersome.

HTH,
-Mike
Nov 14 '05 #15
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:u%******************@newsread2.news.pas.earth link.net...
"silveira neto" <ot**********@yahoo.com.br> wrote in message
int main(){


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


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


Yes, it is. If you don't explicitly return (or exit()) something from
main(), you may implicitly return a trap value.

Richard
Nov 14 '05 #16
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40****************@news.individual.net>...
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:u%******************@newsread2.news.pas.earth link.net...
"silveira neto" <ot**********@yahoo.com.br> wrote in message
> int main(){

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


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


Yes, it is. If you don't explicitly return (or exit()) something from
main(), you may implicitly return a trap value.


There aren't any trap representations in C89.

--
Peter
Nov 14 '05 #17
ai***@acay.com.au (Peter Nilsson) wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40****************@news.individual.net>...
"Peter Nilsson" <ai***@acay.com.au> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:u%******************@newsread2.news.pas.earth link.net...
> "silveira neto" <ot**********@yahoo.com.br> wrote in message
> > int main(){
>
> return 0; /* mandatory for C89, optional for C99 */
> > }

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


Yes, it is. If you don't explicitly return (or exit()) something from
main(), you may implicitly return a trap value.


There aren't any trap representations in C89.


This is contrary to what several experts, not the least of them, claimed
in c.l.c some time ago when I asked about it.

Richard
Nov 14 '05 #18

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

Similar topics

14
18797
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...
6
2561
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
2927
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...
10
93799
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...
0
1360
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...
14
3259
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...
8
4213
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...
3
2878
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...
7
5007
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...
0
6908
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...
0
7045
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,...
1
6741
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...
0
6944
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...
0
5341
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,...
0
4483
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...
0
2995
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...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...

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.