TIP: Using a delay in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silveira neto

    #1

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

    }
    }


  • Mike Wahler

    #2
    Re: Using a delay in C

    "silveira neto" <otenarievlis@y ahoo.com.br> wrote in message
    news:be0f529c92 4fef3f7a13dbe24 b592fef@localho st.talkaboutpro gramming.com...[color=blue]
    > 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.[/color]

    What you have below is not (standard) C.
    [color=blue]
    >
    > #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");[/color]

    This string argument to 'printf()' depends upon the standard
    input device interpreting it a particular way. Not portable at all.
    [color=blue]
    > usleep(50000);[/color]

    No such function in standard C.
    [color=blue]
    > branco(i);
    > printf(" __@\n");
    > branco(i);
    > printf(" _`\\<,_\n");
    > branco(i);
    > printf(" (*)/ (*)\n");
    >
    > }[/color]

    return 0; /* mandatory for C89, optional for C99 */
    [color=blue]
    > }[/color]

    A 'cute' little program only usable on a limited number
    of platforms/configurations.

    -Mike


    Comment

    • E. Robert Tisdale

      #3
      Re: TIP: Using a delay in C

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

      Comment

      • Peter Nilsson

        #4
        Re: Using a delay in C

        "silveira neto" <otenarievlis@y ahoo.com.br> wrote in message
        news:be0f529c92 4fef3f7a13dbe24 b592fef@localho st.talkaboutpro gramming.com...[color=blue]
        > Using a delay in C, using Linux. gcc-3.3[/color]

        This group is about ISO C, i.e. programs which compile and run on _all_
        platforms, not just Linux using GNU C.
        [color=blue]
        > This program do a boy in a bike runing in the screen. :D
        > Its cool to see about games and delay in C.[/color]

        Please post such unix specific 'tips' to unix specific groups.
        [color=blue]
        > <snip>[/color]

        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


        Comment

        • Peter Nilsson

          #5
          Re: Using a delay in C

          "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
          news:u%GQb.2553 9$1e.15965@news read2.news.pas. earthlink.net.. .[color=blue]
          > "silveira neto" <otenarievlis@y ahoo.com.br> wrote in message
          >[/color]
          news:be0f529c92 4fef3f7a13dbe24 b592fef@localho st.talkaboutpro gramming.com...[color=blue][color=green]
          > >
          > > int main(){[/color][/color]
          ....[color=blue]
          >
          > return 0; /* mandatory for C89, optional for C99 */[color=green]
          > > }[/color][/color]

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

          --
          Peter


          Comment

          • Severian

            #6
            Re: Using a delay in C

            On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
            <airia@acay.com .au> wrote:
            [color=blue]
            > while (clock() != end) ;[/color]

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

            - Sev

            Comment

            • E. Robert Tisdale

              #7
              Re: Using a delay in C

              Peter Nilsson wrote:
              [color=blue]
              > silveira neto wrote:
              >[color=green]
              >>Using a delay in C, using Linux. gcc-3.3[/color]
              >
              > This group is about ISO C,
              > i.e. programs which compile and run on _all_ platforms,
              > not just Linux using GNU C.[/color]

              Nonsense!
              There are almost *no* useful C programs
              which compile and run on _all_ platforms.
              [color=blue][color=green]
              >>This program do a boy in a bike running in the screen. :D
              >>It's cool to see about games and delay in C.[/color]
              >
              > 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;
              > }[/color]

              It's more portable. And even more readable.
              But it sucks compared to the original when I run it.

              Comment

              • Peter Nilsson

                #8
                Re: Using a delay in C

                "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                news:4013463D.9 070209@jpl.nasa .gov...[color=blue]
                > Peter Nilsson wrote:
                >[color=green]
                > > silveira neto wrote:
                > >[color=darkred]
                > >>Using a delay in C, using Linux. gcc-3.3[/color]
                > >
                > > This group is about ISO C,
                > > i.e. programs which compile and run on _all_ platforms,
                > > not just Linux using GNU C.[/color]
                >
                > Nonsense!
                > There are almost *no* useful C programs
                > which compile and run on _all_ platforms.[/color]

                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.
                [color=blue]
                > <snip ISO variation>
                >
                > It's more portable. And even more readable.
                > But it sucks compared to the original when I run it.[/color]

                Maybe next time, I'll allocate the time to output a DVD quality mpeg file.

                --
                Peter


                Comment

                • Peter Nilsson

                  #9
                  Re: Using a delay in C

                  "Severian" <severian@chlam ydia-is-not-a-flower.com> wrote in message
                  news:8ig610l6li vhe2t93g7et80se a51t07jn9@4ax.c om...[color=blue]
                  > On Sun, 25 Jan 2004 15:05:20 +1100, "Peter Nilsson"
                  > <airia@acay.com .au> wrote:
                  >[color=green]
                  > > while (clock() != end) ;[/color]
                  >
                  > You may be spinning for a long time. I would recommend < instead of
                  > !=.[/color]

                  True! My port of gcc has CLOCKS_PER_SEC of 93 and a resolution of 5!

                  --
                  Peter


                  Comment

                  • Emmanuel Delahaye

                    #10
                    Re: Using a delay in C

                    In 'comp.lang.c', "Peter Nilsson" <airia@acay.com .au> wrote:
                    [color=blue][color=green][color=darkred]
                    >> > int main(){[/color][/color]
                    > ...[color=green]
                    >> return 0; /* mandatory for C89, optional for C99 */[color=darkred]
                    >> > }[/color][/color]
                    >
                    > It's not _mandatory_ in C89 in the strictest sense, merely desirable.[/color]

                    I think it is (mandatory), unless, the returned value is not defined (some
                    platform force 0, but it's not a C90 rule).

                    --
                    -ed- emdelYOURBRA@no os.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/

                    Comment

                    • silveira neto

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

                      Comment

                      • silveira neto

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

                        Comment

                        • silveira neto

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

                          Comment

                          • silveira neto

                            #14
                            sorry

                            ehr, sorry for my tripple post! :P
                            nothing happend, so I pushed f5 sometimes.

                            Comment

                            • Mike Wahler

                              #15
                              Re: Using a delay in C

                              "silveira neto" <otenarievlis@y ahoo.com.br> wrote in message
                              news:934625df9c b606dbd24f428a8 03d4112@localho st.talkaboutpro gramming.com...[color=blue]
                              > First, thanks for the atention.
                              > Sorry for my faults in the program, I learned C the day before.[/color]

                              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).
                              [color=blue]
                              > And sorry for some words in portuguese, my live in Brazil, I forget[/color]
                              translate in the source.

                              Not a problem.
                              [color=blue]
                              > So, I think is important the portability of the source.[/color]

                              It can be important in many cases, yes.
                              [color=blue]
                              >I would know where I can learn about the C that I can write to all
                              >plataforms, if this is possible.[/color]

                              It is possible. Learn how from good books.
                              See www.accu.org for peer reviews by the experts.
                              [color=blue]
                              > What C is this C that we are talking about?[/color]

                              The language which is the topic of comp.lang.c is the standard
                              C language as defined by the international standard ISO/IEC 9899.

                              [color=blue]
                              >
                              > I compiled the remaked program, thanks Peter Nilsson, but as Robert[/color]
                              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


                              Comment

                              Working...