K&R Ex 1-3

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

    K&R Ex 1-3

    I'm reading the K&R book and doing the exercises. Currently, I'm working
    on Exercise 1-3.

    Here's what I have:

    Code:
    #include <stdio.h>
    
    main()
    {
    float fahr, celsius;
    float lower, upper, step;
    
    lower = 0;   /* lower limit of temperatuire scale */
    upper = 300; /* upper limit */
    step = 20;   /* step size */
    fahr = lower;
    
    printf("F\t\tC\n");
    while (fahr <= upper) {
    celsius = (5.0/9.0) * (fahr-32.0);
    printf("%4.0f %7.1f\n", fahr, celsius);
    fahr = fahr + step;
    }
    }
    Now, this completes the exercise. Technically, anyway. However, I want
    to make the "F" and "C" align properly above the right side of their
    respective columns.

    I realize this is probably a really minor thing, but I'd appreciate the
    help none-the-less!

    - Gio

    --
    AND NOW FOR A WORD (an IF blog):


  • Ian Collins

    #2
    Re: K&amp;R Ex 1-3

    Gio wrote:
    I'm reading the K&R book and doing the exercises. Currently, I'm working
    on Exercise 1-3.
    >
    Here's what I have:
    >
    Code:
    >
    #include <stdio.h>
    >
    main()
    Code:
    make that int main(void).
    
    While you are there, turn  your compiler's warnings up, the above should
    have caused one.
    [QUOTE]
    {
        float fahr, celsius;
        float lower, upper, step;
    >
        lower = 0;   /* lower limit of temperatuire scale */
        upper = 300; /* upper limit */
        step = 20;   /* step size */
        fahr = lower;
    >
        printf("F\t\tC\n");
        while (fahr <= upper) {
            celsius = (5.0/9.0) * (fahr-32.0);
            printf("%4.0f %7.1f\n", fahr, celsius);
            fahr = fahr + step;
        }
    }
    >
    [/QUOTE]
    >
    Now, this completes the exercise. Technically, anyway. However, I want
    to make the "F" and "C" align properly above the right side of their
    respective columns.
    >
    Just add some spaces to the first print and a couple of tabs to the second.

    printf(" F\t\t C\n");
    printf("%4.0f\t \t%7.1f\n", fahr, celsius);

    --
    Ian Collins.

    Comment

    • Ben Bacarisse

      #3
      Re: K&amp;R Ex 1-3

      Gio <giomancer@sbcg lobal.netwrites :
      I'm reading the K&R book and doing the exercises. Currently, I'm
      working on Exercise 1-3.
      <snip>
      printf("F\t\tC\ n");
      while (fahr <= upper) {
      celsius = (5.0/9.0) * (fahr-32.0);
      printf("%4.0f %7.1f\n", fahr, celsius);
      fahr = fahr + step;
      }
      <snip>
      Now, this completes the exercise. Technically, anyway. However, I want
      to make the "F" and "C" align properly above the right side of their
      respective columns.
      I like to do this using the same sizes you have in the numeric
      format:

      printf("%4s %7s\n", "F", "C");

      It keeps everything clear, and you can even wire in a configurable size
      from a macro if you really want to (although that is a serious case of
      overkill in this exercise).

      --
      Ben.

      Comment

      • Gio

        #4
        Re: K&amp;R Ex 1-3

        While you are there, turn your compiler's warnings up, the above
        should have caused one.
        Well.. ah.. I'm working on an Apple II; the compiler I ran this program
        on is technically pre-ANSI, but I've kept the differences in mind (and
        the documentation close at hand). I figured I could ask a K&R question,
        though.

        As to why I'm learning C on an Apple IIe.. I'm doing it to get used to
        the compiler and the limitations of the computer. And because I'm
        slightly nuts. :)

        - Gio

        --
        AND NOW FOR A WORD (an IF blog):


        Comment

        • Richard Heathfield

          #5
          Re: K&amp;R Ex 1-3

          Ian Collins said:
          Gio wrote:
          <snip>
          >main()
          >
          make that int main(void).
          >
          While you are there, turn your compiler's warnings up, the above should
          have caused one.
          Why?

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Richard Heathfield

            #6
            Re: K&amp;R Ex 1-3

            Gio said:
            >While you are there, turn your compiler's warnings up, the above
            >should have caused one.
            >
            Well.. ah.. I'm working on an Apple II; the compiler I ran this program
            on is technically pre-ANSI, but I've kept the differences in mind (and
            the documentation close at hand). I figured I could ask a K&R question,
            though.
            You can. K&R C is topical here, although some people here tend to forget
            that.
            >
            As to why I'm learning C on an Apple IIe.. I'm doing it to get used to
            the compiler and the limitations of the computer. And because I'm
            slightly nuts. :)
            ....and of course it is *not* comp.lang.c's place to dictate to you what
            compiler you must use. Some of the people here occasionally forget that,
            too.

            (There are, however, definite advantages to be had in bringing yourself
            kicking and screaming into the late 1980s. The ANSI C Standard of 1989 was
            a significant step forward for C.)

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Gio

              #7
              Re: K&amp;R Ex 1-3

              printf("%4s %7s\n", "F", "C");

              Aha! This is perfect for me, thank you! I didn't know that it could work
              like that. :p

              - Gio

              --
              AND NOW FOR A WORD (an IF blog):


              Comment

              • Ian Collins

                #8
                Re: K&amp;R Ex 1-3

                Richard Heathfield wrote:
                Ian Collins said:
                >
                >Gio wrote:
                >
                <snip>
                >
                >>main()
                >make that int main(void).
                >>
                >While you are there, turn your compiler's warnings up, the above should
                >have caused one.
                >
                Why?
                >
                You know why.

                --
                Ian Collins.

                Comment

                • Richard Heathfield

                  #9
                  Re: K&amp;R Ex 1-3

                  Ian Collins said:
                  Richard Heathfield wrote:
                  >Ian Collins said:
                  >>
                  >>Gio wrote:
                  >>
                  ><snip>
                  >>
                  >>>main()
                  >>make that int main(void).
                  >>>
                  >>While you are there, turn your compiler's warnings up, the above
                  >>should have caused one.
                  >>
                  >Why?
                  >>
                  You know why.
                  If I knew why, I wouldn't need to ask why. I can see no reason why main()
                  should require a K&R C implementation (such as the OP is using) to issue a
                  diagnostic message. Please explain.

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • Ian Collins

                    #10
                    Re: K&amp;R Ex 1-3

                    Richard Heathfield wrote:
                    Ian Collins said:
                    >
                    >Richard Heathfield wrote:
                    >>Ian Collins said:
                    >>>
                    >>>Gio wrote:
                    >><snip>
                    >>>
                    >>>>main()
                    >>>make that int main(void).
                    >>>>
                    >>>While you are there, turn your compiler's warnings up, the above
                    >>>should have caused one.
                    >>Why?
                    >>>
                    >You know why.
                    >
                    If I knew why, I wouldn't need to ask why. I can see no reason why main()
                    should require a K&R C implementation (such as the OP is using) to issue a
                    diagnostic message. Please explain.
                    >
                    The OP didn't say he was using such an old compiler until his reply to
                    my post.

                    --
                    Ian Collins.

                    Comment

                    • Bill Buckels

                      #11
                      Re: K&amp;R Ex 1-3

                      On Apr 19, 8:33 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                      Richard Heathfield wrote:
                      Ian Collins said:
                      >
                      Gio wrote:
                      >
                      <snip>
                      >
                      >main()
                      make that int main(void).
                      >
                      While you are there, turn  your compiler's warnings up, the above should
                      have caused one.
                      >
                      Why?
                      >
                      You know why.
                      >
                      --
                      Ian Collins.
                      Not on a K&R Compiler. Also warning levels not available on this
                      particular compiler, however 60% of Apple II developers wrote in Aztec
                      C. During the mid 80's Aztec C was the largesty selling C compiler.
                      And the best C compiler of the 80's and perhaps of all time.

                      Go to the Aztec C Website and take a look:



                      Bill Bucxels

                      Comment

                      • Ian Collins

                        #12
                        Re: K&amp;R Ex 1-3

                        Bill Buckels wrote:
                        On Apr 19, 8:33 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                        >Richard Heathfield wrote:
                        >>Ian Collins said:
                        >>>Gio wrote:
                        >><snip>
                        >>>>main()
                        >>>make that int main(void).
                        >>>While you are there, turn your compiler's warnings up, the above should
                        >>>have caused one.
                        >>Why?
                        >You know why.
                        >>
                        *Please* don't quote signatures.
                        >
                        Not on a K&R Compiler.
                        See my reply to Richard.

                        --
                        Ian Collins.

                        Comment

                        • Richard Heathfield

                          #13
                          Re: K&amp;R Ex 1-3

                          Ian Collins said:
                          Richard Heathfield wrote:
                          >Ian Collins said:
                          >>
                          >>Richard Heathfield wrote:
                          >>>Ian Collins said:
                          >>>>
                          >>>>Gio wrote:
                          >>><snip>
                          >>>>
                          >>>>>main()
                          >>>>make that int main(void).
                          >>>>>
                          >>>>While you are there, turn your compiler's warnings up, the above
                          >>>>should have caused one.
                          >>>Why?
                          >>>>
                          >>You know why.
                          >>
                          >If I knew why, I wouldn't need to ask why. I can see no reason why
                          >main() should require a K&R C implementation (such as the OP is using)
                          >to issue a diagnostic message. Please explain.
                          >>
                          The OP didn't say he was using such an old compiler until his reply to
                          my post.
                          <shrugThat would be fair enough, if the same point didn't apply to C90
                          implementations . It would appear from your reply that you assumed, against
                          the balance of reasonable probability, that he was using a conforming C99
                          implementation!

                          Look, if your point is that the int main(void) form is *preferable*, I
                          agree completely. I use that form myself and recommend it to others. But
                          there's quite a lot of distance between "this form is preferred to that
                          form" and "that form should have caused the implementation to issue a
                          diagnostic message".

                          Furthermore, K&R C implementations do not, as far as I'm aware, necessarily
                          support the void keyword (although I accept that it is not reasonable to
                          expect you to have taken *that* into account in your original reply).

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Gio

                            #14
                            Re: K&amp;R Ex 1-3

                            There are, however, definite advantages to be had in bringing
                            yourself kicking and screaming into the late 1980s. The ANSI C
                            Standard of 1989 was a significant step forward for C.
                            *nods* My first experience with coding was a CS class in college; they
                            taught ANSI C (ISO C was, I think, a year away). The person that makes
                            the compiler I'm using (Aztec-C) available has been talking to the
                            company that owns it to see if he can get the source and update it to
                            ANSI standards.

                            There is another compiler for the Apple IIe called cc65. It's not too
                            bad, really, and it is (according to the webpage) almost conforming to
                            the ISO standard. The program from the K&R book would not compile in it
                            without modification. Unfortunately, I can't get its output file to run
                            under ProDOS, which is the OS of choice for the Apple IIe.

                            While that is probably a personal problem, I'm going down the path of
                            least resistance for now. :)

                            - Gio

                            --
                            AND NOW FOR A WORD (an IF blog):


                            Comment

                            • Keith Thompson

                              #15
                              Re: K&amp;R Ex 1-3

                              Ian Collins <ian-news@hotmail.co mwrites:
                              Gio wrote:
                              >I'm reading the K&R book and doing the exercises. Currently, I'm working
                              >on Exercise 1-3.
                              >>
                              >Here's what I have:
                              >>
                              >[code]
                              >>
                              >#include <stdio.h>
                              >>
                              >main()
                              >
                              make that int main(void).
                              >
                              While you are there, turn your compiler's warnings up, the above should
                              have caused one.
                              [...]

                              For certain values of "should". A C90-conforming implementation is
                              not required to issue a diagnostic for that declaration of main,
                              though a decent one will do so anyway if you ask it nicely.

                              --
                              Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...