Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > TIP: Using a delay in C

Reply
Thread Tools

TIP: Using a delay in C

 
 
silveira neto
Guest
Posts: n/a
 
      01-25-2004
Using a delay in C, using Linux. gcc-3.3
This program do a boy in a bike runing in the screen.
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");

}
}


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-25-2004
"silveira neto" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) lkaboutprogramming.com...
> Using a delay in C, using Linux. gcc-3.3
> This program do a boy in a bike runing in the screen.
> 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


 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-25-2004
> 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;
}

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"silveira neto" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
> 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


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"Mike Wahler" <(E-Mail Removed)> wrote in message
news:u%GQb.25539$(E-Mail Removed) link.net...
> "silveira neto" <(E-Mail Removed)> wrote in message
>

news:(E-Mail Removed) 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


 
Reply With Quote
 
Severian
Guest
Posts: n/a
 
      01-25-2004
On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
<(E-Mail Removed)> wrote:

> while (clock() != end) ;


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

- Sev

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-25-2004
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.
>>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.

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"Severian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
> <(E-Mail Removed)> 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


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-25-2004
In 'comp.lang.c', "Peter Nilsson" <(E-Mail Removed)> 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- http://www.velocityreviews.com/forums/(E-Mail Removed) [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/
 
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
Calculating propagation delay & transmission delay Stone Cisco 1 09-27-2006 06:26 PM
delay using integrator Sarah VHDL 2 02-19-2006 04:35 AM
Using Sipgate, short delay before calling person can hear me. GwG UK VOIP 7 12-24-2005 10:50 PM
Long delay using SSLSocketFactory Thomas Mantay Java 0 04-26-2004 01:40 PM
Delay Web Request using HttpRuntime.ProcessRequest Ross Dempster ASP .Net 0 07-18-2003 03:30 PM



Advertisments