TIP: Using a delay in C

S

silveira neto

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");

}
}
 
M

Mike Wahler

silveira neto said:
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
 
E

E. Robert Tisdale

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;
}
 
P

Peter Nilsson

silveira neto said:
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.

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;
}
 
P

Peter Nilsson

Mike Wahler said:
news:be0f529c924fef3f7a13dbe24b592fef@localhost.talkaboutprogramming.com... ....

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

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

E. Robert Tisdale

Peter said:
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.
 
P

Peter Nilsson

E. Robert Tisdale said:
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.
 
P

Peter Nilsson

Severian said:
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!
 
E

Emmanuel Delahaye

Peter Nilsson said:
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).
 
S

silveira neto

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

silveira neto

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

silveira neto

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

Mike Wahler

silveira neto said:
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
 
R

Richard Bos

Peter Nilsson said:
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
 
P

Peter Nilsson

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

Richard Bos

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top