question

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

    #1

    question

    Just picked up on c programming again and doing
    Kernighan & Ritchie c programming

    the question is this why does the program below
    not work;

    #include <stdio.h>

    /* count characters in input: 1st version */

    main()
    {
            long nc;

            nc = 0;
           
            while(getchar() != EOF)
              ++nc;
             printf("%ld\n", nc);
           
    }

    typed as in book

    to get it to work i have to do this:

    #include <stdio.h>

    /* count characters in input: 1st version */

    int main(void)
    {
            long nc;

            nc = 0;

            while(getchar() != EOF)
            {
              ++nc;
              printf("%ld\n", nc);
            }
            return 0;
    }


    could the answer be that the programs in the above book
    was wrote for the old compilers a detailed explanation
    would be most helpful thanks for your help
    #include <stdio.h>
    >
    /* count characters in input: 1st version */
    >
    int main(void)
    {
            long nc;
    >
            nc = 0;
    >
            while(getchar() != EOF)
            {
              ++nc;
              printf("%ld\n", nc);
            }
            return 0;
    }
    >
    owe should this be the out put to the above program:

    user@linux-1669:~/cpro/ch1./count
    user
    1
    2
    3
    4
    5
  • Lowell Gilbert

    #2
    Re: question

    darklight <nglen702@netsc ape.netwrites:
    the question is this why does the program below
    not work;
    I don't know. It looks and works okay as far as I'm concerned.

    What do you think is wrong? I notice that some of your changes in
    your modified version alter the behaviour of the program, which
    originally only printed the length once, after all of the input had
    been read.

    Comment

    • Andrew Poelstra

      #3
      Re: question

      On 2006-08-16, darklight <nglen702@netsc ape.netwrote:
      Just picked up on c programming again and doing
      Kernighan & Ritchie c programming
      >
      the question is this why does the program below
      not work;
      >
      #include <stdio.h>
      >
      /* count characters in input: 1st version */
      >
      main()
      Main returns int now. What version of K&R are you using? The 1st one isn't
      all that useful for learning C. (Whether it's useful at all I don't know.)

      int main (void)
      {
              long nc;
              nc = 0;
      These could be combined into
      long nc = 0;
      just so you know.
             
              while(getchar() != EOF)
                ++nc;
               printf("%ld\n", nc);
      >
      Since main returns an int, you should have a
      return 0;
      here.
      }
      >
      typed as in book
      >
      to get it to work i have to do this:
      >
      #include <stdio.h>
      >
      /* count characters in input: 1st version */
      >
      int main(void)
      {
              long nc;
      >
              nc = 0;
      >
              while(getchar() != EOF)
              {
                ++nc;
                printf("%ld\n", nc);
              }
              return 0;
      }
      >
      There! See, you fixed it all.
      >
      could the answer be that the programs in the above book
      was wrote for the old compilers a detailed explanation
      would be most helpful thanks for your help
      >
      Now even an old compiler; the original K&R was written even
      before the first /standard/ of C. Hence, it has a lot of
      weird conventions that aren't valid today. (Also, the 2nd
      version's example listings have been changed in unrelated
      ways to be clearer, cleaner, or conciser.)

      --
      Andrew Poelstra <http://www.wpsoftware. net/projects>
      To reach me by email, use `apoelstra' at the above domain.
      "Do BOTH ends of the cable need to be plugged in?" -Anon.

      Comment

      • Ancient_Hacker

        #4
        Re: question


        darklight wrote:
        Just picked up on c programming again and doing
        Kernighan & Ritchie c programming
        >
        the question is this why does the program below
        not work;
        Depends what you mean by "work".

        Different implementations of the C I/O library will buffer the input
        and output in differnt ways, so your input and output may not appear at
        the expected times.

        Comment

        • osmium

          #5
          Re: question

          "Andrew Poelstra" writes:
          >main()
          >
          Main returns int now. What version of K&R are you using? The 1st one isn't
          all that useful for learning C. (Whether it's useful at all I don't know.)
          That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
          you can actually look at it, instead of speculating on what it might say, or
          what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
          same text is available with other ISBNs as well.


          Comment

          • Coos Haak

            #6
            Re: question

            Op Wed, 16 Aug 2006 08:17:34 -0700 schreef osmium:
            "Andrew Poelstra" writes:
            >
            >>main()
            >>
            >Main returns int now. What version of K&R are you using? The 1st one isn't
            >all that useful for learning C. (Whether it's useful at all I don't know.)
            >
            That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
            you can actually look at it, instead of speculating on what it might say, or
            what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
            same text is available with other ISBNs as well.
            It's a direct copy of page 18 in the second edition.
            ISB number 0-13-110362-8 or 0-13-110370-9
            Which version do you have?
            --
            Coos

            Comment

            • osmium

              #7
              Re: question

              "Coos Haak" writes:
              Op Wed, 16 Aug 2006 08:17:34 -0700 schreef osmium:
              >
              >"Andrew Poelstra" writes:
              >>
              >>>main()
              >>>
              >>Main returns int now. What version of K&R are you using? The 1st one
              >>isn't
              >>all that useful for learning C. (Whether it's useful at all I don't
              >>know.)
              >>
              >That's a direct copy from K&R ANSI C on p 59. If you care to get a copy
              >so
              >you can actually look at it, instead of speculating on what it might say,
              >or
              >what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
              >same text is available with other ISBNs as well.
              >
              It's a direct copy of page 18 in the second edition.
              ISB number 0-13-110362-8 or 0-13-110370-9
              Which version do you have?
              I have neither, I have the one I described in my post. Polestra, was
              harping on the implicit int return type, what followed that was immaterial.
              I just opened K&R to a random page, and simply mentioned that page as an
              example. Disregarding bindings, natural language used, and so on, there are
              only two K&Rs. The book I described is as good as it gets, ANSI C. All
              good pedants should have a copy at their fingertips.


              Comment

              • Martin Ambuhl

                #8
                Re: question

                Andrew Poelstra wrote:
                On 2006-08-16, darklight <nglen702@netsc ape.netwrote:
                >Just picked up on c programming again and doing
                >Kernighan & Ritchie c programming
                >>
                >the question is this why does the program below
                >not work;
                >>
                >#include <stdio.h>
                >>
                >/* count characters in input: 1st version */
                >>
                >main()
                >
                Main returns int now. What version of K&R are you using?
                In the version given, all versions of C before C99 had main returning an
                int. This is not a question of whether "Main returns int now." The
                "now" is bogus. With versions of C before C99, implicit int was
                supported and
                main() { /* ... */ }
                has exactly the same return type as
                int main() { /* ... */ }
                The 1st one isn't
                all that useful for learning C.
                It was certainly good enough for me to learn C from in 1978. Many other
                people also learned C from K&R in that era. has the intelligence of
                would-be programmers dropped that much in the last 28 years?

                Comment

                • Andrew Poelstra

                  #9
                  Re: question &lt;OT: K&amp;R Versions&gt;

                  On 2006-08-16, osmium <r124c4u102@com cast.netwrote:
                  "Andrew Poelstra" writes:
                  >
                  >>main()
                  >>
                  >Main returns int now. What version of K&R are you using? The 1st one isn't
                  >all that useful for learning C. (Whether it's useful at all I don't know.)
                  >
                  That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
                  you can actually look at it, instead of speculating on what it might say, or
                  what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
                  same text is available with other ISBNs as well.
                  >
                  I'm not speculating; I merely asked a question. Had I said something like
                  "I think you're using K&R1" or "Why are you using K&R1?" it would have been
                  speculating.

                  According to the eBay guy who sold me K&R1 with the guise of being the 2nd
                  version, both books have the same ISBN, and therefore the same default
                  display picture.

                  --
                  Andrew Poelstra <http://www.wpsoftware. net/projects>
                  To reach me by email, use `apoelstra' at the above domain.
                  "Do BOTH ends of the cable need to be plugged in?" -Anon.

                  Comment

                  • Andrew Poelstra

                    #10
                    Re: question

                    On 2006-08-16, osmium <r124c4u102@com cast.netwrote:
                    "Coos Haak" writes:
                    >Op Wed, 16 Aug 2006 08:17:34 -0700 schreef osmium:
                    >>"Andrew Poelstra" writes:
                    >>>>main()
                    >>>>
                    >>>Main returns int now. What version of K&R are you using? The 1st one
                    >>>isn't
                    >>>all that useful for learning C. (Whether it's useful at all I don't
                    >>>know.)
                    >>>
                    >>That's a direct copy from K&R ANSI C on p 59. If you care to get a copy
                    >>so
                    >>you can actually look at it, instead of speculating on what it might say,
                    >>or
                    >>what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
                    >>same text is available with other ISBNs as well.
                    >>
                    >It's a direct copy of page 18 in the second edition.
                    >ISB number 0-13-110362-8 or 0-13-110370-9
                    >Which version do you have?
                    >
                    I have neither, I have the one I described in my post. Polestra, was
                    harping on the implicit int return type, what followed that was immaterial.
                    I just opened K&R to a random page, and simply mentioned that page as an
                    example. Disregarding bindings, natural language used, and so on, there are
                    only two K&Rs. The book I described is as good as it gets, ANSI C. All
                    good pedants should have a copy at their fingertips.
                    >
                    All I have is K&R1. I did try to get the second version, but due to false
                    advertising (which in turn was due to both K&R's *apparently* having the
                    same ISBN), I recieved the first. $20 I'll never get back.


                    What I posted was not immaterial, nor was it speculation. It was an honest
                    question.

                    I am not a pedant, nor am I trying to be, because I'm clearly not yet
                    qualified to be one. :-)

                    --
                    Andrew Poelstra <http://www.wpsoftware. net/projects>
                    To reach me by email, use `apoelstra' at the above domain.
                    "Do BOTH ends of the cable need to be plugged in?" -Anon.

                    Comment

                    • Andrew Poelstra

                      #11
                      Re: question &lt;OT: Programmer intellect.&gt;

                      On 2006-08-16, Martin Ambuhl <mambuhl@earthl ink.netwrote:
                      Andrew Poelstra wrote:
                      >On 2006-08-16, darklight <nglen702@netsc ape.netwrote:
                      >>Just picked up on c programming again and doing
                      >>Kernighan & Ritchie c programming
                      >>>
                      >>the question is this why does the program below
                      >>not work;
                      >>>
                      >>#include <stdio.h>
                      >>>
                      >>/* count characters in input: 1st version */
                      >>>
                      >>main()
                      >>
                      >Main returns int now. What version of K&R are you using?
                      >
                      In the version given, all versions of C before C99 had main returning an
                      int. This is not a question of whether "Main returns int now." The
                      "now" is bogus. With versions of C before C99, implicit int was
                      supported and
                      main() { /* ... */ }
                      has exactly the same return type as
                      int main() { /* ... */ }
                      >
                      >The 1st one isn't
                      >all that useful for learning C.
                      >
                      It was certainly good enough for me to learn C from in 1978. Many other
                      people also learned C from K&R in that era. has the intelligence of
                      would-be programmers dropped that much in the last 28 years?
                      Mine hasn't (although my ownership of books is less than most people), but
                      schools now teach basically nothing but Java, and I read through the
                      curriculum of a high school programming course: they teach VB up to and
                      including array syntax. Having been overqualified for said course from
                      about a week after starting programming, I didn't take it.

                      So, yeah, 20 years ago you had to be a /lot/ smarter to be considered a
                      programmer. (With .NET I hear that you don't even need to touch a line
                      of code to "program"!)

                      --
                      Andrew Poelstra <http://www.wpsoftware. net/projects>
                      To reach me by email, use `apoelstra' at the above domain.
                      "Do BOTH ends of the cable need to be plugged in?" -Anon.

                      Comment

                      • Keith Thompson

                        #12
                        Re: question

                        Andrew Poelstra <apoelstra@fals e.sitewrites:
                        On 2006-08-16, darklight <nglen702@netsc ape.netwrote:
                        >Just picked up on c programming again and doing
                        >Kernighan & Ritchie c programming
                        >>
                        >the question is this why does the program below
                        >not work;
                        >>
                        >#include <stdio.h>
                        >>
                        >/* count characters in input: 1st version */
                        >>
                        >main()
                        >
                        Main returns int now. What version of K&R are you using? The 1st one isn't
                        all that useful for learning C. (Whether it's useful at all I don't know.)
                        Implicit int wasn't banned until C99. K&R2 only covers C89/C90.

                        "int main(void)" is better than "main()", but either one should be
                        accepted by any compiler that's not being used in strict C99 mode.

                        [...]
                        Since main returns an int, you should have a
                        return 0;
                        here.
                        Yes, but it's not actually required in any version of C.

                        If I recall correctly, most of the examples in K&R1 don't bother
                        returning an explicit value from main().

                        In C90, falling off the end of main() returns an unspecified status to
                        the calling environment, but it doesn't invoke undefined behavior.

                        In C99, falling off the end of main() does an implicit "return 0;".

                        Adding the "return 0;" is certainly a good idea, but it's not likely to
                        be the cause of the OP's problem.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Default User

                          #13
                          Re: question &lt;OT: K&amp;R Versions&gt;

                          Andrew Poelstra wrote:

                          According to the eBay guy who sold me K&R1 with the guise of being
                          the 2nd version, both books have the same ISBN, and therefore the
                          same default display picture.
                          My research indicates that the ISBN for the first edition was
                          0131101633:

                          The C programming language
                          Brian W. Kernighan, Dennis M. Ritchie
                          Publisher: Englewood Cliffs, N.J. : Prentice-Hall, c1978.
                          ISBN: 0131101633 DDC: 1.6424 LCC: QA76.73
                          http://isbndb.com/d/book/the_c_progr..._language.html



                          The second was 0131103628:

                          The C programming language
                          Brian W. Kernighan, Dennis M. Ritchie
                          Publisher: Englewood Cliffs, N.J. : Prentice Hall, c1988.
                          ISBN: 0131103628 DDC: 5.133 LCC: QA76.73 Edition: (pbk.)
                          http://isbndb.com/d/book/the_c_progr...guage_a05.html





                          Brian

                          Comment

                          • Keith Thompson

                            #14
                            Re: question

                            darklight <nglen702@netsc ape.netwrites:
                            Just picked up on c programming again and doing
                            Kernighan & Ritchie c programming
                            >
                            the question is this why does the program below
                            not work;
                            No, the question is *how* does the program below not work.
                            #include <stdio.h>
                            >
                            /* count characters in input: 1st version */
                            >
                            main()
                            {
                                    long nc;
                            >
                                    nc = 0;
                                   
                                    while(getchar() != EOF)
                                      ++nc;
                                     printf("%ld\n", nc);
                                   
                            }
                            >
                            typed as in book
                            So what *exactly* happened when you tried it?

                            I can compile and execute it without error. It has some archaic
                            constructs, but any compiler not being used in strict C99 mode should
                            accept it, perhaps with a warning or two.

                            One thing to watch out for is that input and output can be buffered.
                            The program doesn't produce any output until it reaches end-of-file.
                            If you're running it with input from the keyboard, that means you have
                            to type some system-specific key sequence to denote end-of-file. (On
                            Unix-like systems, type control-D at the beginning of a line, or
                            control-D twice in the middle of a line. In Windows or MS-DOS, use
                            control-Z.)

                            The subject header of your message, "question", doesn't give us any
                            clue what you're asking about. The subject should briefly describe
                            the problem; if it doesn't, a lot of people are going to skip over it
                            without reading it.

                            If something doesn't work, you need to tell us exactly *how* it
                            doesn't work. Does it fail to compile? Do you get any error
                            messages? If so, *exactly* what are they?

                            I recommend reading <http://www.catb.org/~esr/faqs/smart-questions.html> .

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • darklight

                              #15
                              Re: question &lt;OT: K&amp;R Versions&gt;

                              Andrew Poelstra wrote:
                              On 2006-08-16, osmium <r124c4u102@com cast.netwrote:
                              >"Andrew Poelstra" writes:
                              >>
                              >>>main()
                              >>>
                              >>Main returns int now. What version of K&R are you using? The 1st one
                              >>isn't all that useful for learning C. (Whether it's useful at all I
                              >>don't know.)
                              >>
                              >That's a direct copy from K&R ANSI C on p 59. If you care to get a copy
                              >so you can actually look at it, instead of speculating on what it might
                              >say, or
                              >what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
                              >same text is available with other ISBNs as well.
                              >>
                              >
                              I'm not speculating; I merely asked a question. Had I said something like
                              "I think you're using K&R1" or "Why are you using K&R1?" it would have
                              been speculating.
                              >
                              According to the eBay guy who sold me K&R1 with the guise of being the 2nd
                              version, both books have the same ISBN, and therefore the same default
                              display picture.
                              >
                              using second edition if that makes any difference

                              Comment

                              Working...