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

  2. silveira neto

    Mike Wahler Guest

    What you have below is not (standard) C.
    This string argument to 'printf()' depends upon the standard
    input device interpreting it a particular way. Not portable at all.
    No such function in standard C.
    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. Advertisements

  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. This group is about ISO C, i.e. programs which compile and run on _all_
    platforms, not just Linux using GNU 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;
    }
     
    Peter Nilsson, Jan 25, 2004
    #4
  5. It's not _mandatory_ in C89 in the strictest sense, merely desirable.
     
    Peter Nilsson, Jan 25, 2004
    #5
  6. silveira neto

    Severian Guest

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

    - Sev
     
    Severian, Jan 25, 2004
    #6
  7. Nonsense!
    There are almost *no* useful C programs
    which compile and run on _all_ platforms.
    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. 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.
    Maybe next time, I'll allocate the time to output a DVD quality mpeg file.
     
    Peter Nilsson, Jan 25, 2004
    #8
  9. True! My port of gcc has CLOCKS_PER_SEC of 93 and a resolution of 5!
     
    Peter Nilsson, Jan 25, 2004
    #9
  10. I think it is (mandatory), unless, the returned value is not defined (some
    platform force 0, but it's not a C90 rule).
     
    Emmanuel Delahaye, Jan 25, 2004
    #10
  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.
     
    silveira neto, Jan 25, 2004
    #11
  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.
     
    silveira neto, Jan 25, 2004
    #12
  13. 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. 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

    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).
    translate in the source.

    Not a problem.
    It can be important in many cases, yes.
    It is possible. Learn how from good books.
    See www.accu.org for peer reviews by the experts.
    The language which is the topic of comp.lang.c is the standard
    C language as defined by the international standard ISO/IEC 9899.

    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

    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. There aren't any trap representations in C89.
     
    Peter Nilsson, Jan 26, 2004
    #17
  18. silveira neto

    Richard Bos Guest

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

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 (here). After that, you can post your question and our members will help you out.