TIP: Using a delay in C

Discussion in 'C Programming' started by silveira neto, Jan 25, 2004.

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

    }
    }
    silveira neto, Jan 25, 2004
    #1
    1. Advertising

  2. silveira neto

    Mike Wahler Guest

    Re: Using a delay in C

    "silveira neto" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 25, 2004
    #2
    1. Advertising

  3. > 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;
    }
    E. Robert Tisdale, Jan 25, 2004
    #3
  4. Re: Using a delay in C

    "silveira neto" <> wrote in message
    news:...
    > 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
    Peter Nilsson, Jan 25, 2004
    #4
  5. Re: Using a delay in C

    "Mike Wahler" <> wrote in message
    news:u%GQb.25539$...
    > "silveira neto" <> wrote in message
    >

    news:...
    > >
    > > int main(){

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


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

    --
    Peter
    Peter Nilsson, Jan 25, 2004
    #5
  6. silveira neto

    Severian Guest

    Re: Using a delay in C

    On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
    <> wrote:

    > while (clock() != end) ;


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

    - Sev
    Severian, Jan 25, 2004
    #6
  7. Re: Using a delay in C

    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.
    E. Robert Tisdale, Jan 25, 2004
    #7
  8. Re: Using a delay in C

    "E. Robert Tisdale" <> wrote in message
    news:...
    > 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
    Peter Nilsson, Jan 25, 2004
    #8
  9. Re: Using a delay in C

    "Severian" <> wrote in message
    news:...
    > On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
    > <> 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
    Peter Nilsson, Jan 25, 2004
    #9
  10. Re: Using a delay in C

    In 'comp.lang.c', "Peter Nilsson" <> 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- [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/
    Emmanuel Delahaye, Jan 25, 2004
    #10
  11. Re: Using a delay in C

    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.
    silveira neto, Jan 25, 2004
    #11
  12. Re: Using a delay in C

    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.
    silveira neto, Jan 25, 2004
    #12
  13. Re: Using a delay in C

    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.
    silveira neto, Jan 25, 2004
    #13
  14. sorry

    ehr, sorry for my tripple post! :p
    nothing happend, so I pushed f5 sometimes.
    silveira neto, Jan 25, 2004
    #14
  15. silveira neto

    Mike Wahler Guest

    Re: Using a delay in C

    "silveira neto" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 25, 2004
    #15
  16. silveira neto

    Richard Bos Guest

    Re: Using a delay in C

    "Peter Nilsson" <> wrote:

    > "Mike Wahler" <> wrote in message
    > news:u%GQb.25539$...
    > > "silveira neto" <> 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
    Richard Bos, Jan 26, 2004
    #16
  17. Re: Using a delay in C

    (Richard Bos) wrote in message news:<>...
    > "Peter Nilsson" <> wrote:
    >
    > > "Mike Wahler" <> wrote in message
    > > news:u%GQb.25539$...
    > > > "silveira neto" <> 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
    Peter Nilsson, Jan 26, 2004
    #17
  18. silveira neto

    Richard Bos Guest

    Re: Using a delay in C

    (Peter Nilsson) wrote:

    > (Richard Bos) wrote in message news:<>...
    > > "Peter Nilsson" <> wrote:
    > >
    > > > "Mike Wahler" <> wrote in message
    > > > news:u%GQb.25539$...
    > > > > "silveira neto" <> 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
    Richard Bos, Jan 27, 2004
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Brown

    Tip: Windows internals using wmi

    Colin Brown, Oct 3, 2003, in forum: Python
    Replies:
    8
    Views:
    1,059
    Tim Golden
    Oct 8, 2003
  2. RG
    Replies:
    0
    Views:
    440
  3. Per Velschow
    Replies:
    5
    Views:
    101
    Per Velschow
    Jan 22, 2007
  4. David Mark
    Replies:
    16
    Views:
    909
    Scott Sauyet
    Nov 11, 2011
  5. David Mark
    Replies:
    58
    Views:
    1,414
    David Mark
    Dec 6, 2011
Loading...

Share This Page