K&r typo

Discussion in 'C Programming' started by Lorenzo Villari, Feb 9, 2004.

  1. Ahem... I think I've found a typo on my copy of The C Programming Language
    Second Edition. On section 1.9 (page 32 on my version, the Italian one ) you
    do find:

    #include <stdio.h>

    #define MAXLINE 1000 /* lunghezza massima di una linea */

    int getline(char line[], int maxline);
    void copy(char to[], char from[]);


    /* stampa la linea di input più lunga */
    main()
    {
    int len; /* lunghezza della linea
    corrente */
    int max; /* massima lunghezza trovata
    sinora */
    char line[MAXLINE]; /* linea di input corrente */
    char longest[MAXLINE]; /* linea più lunga salvata qui */

    max = 0;

    while((len = getline(line, MAXLINE)) > 0)
    if(len > max)
    {
    max = len;
    copy(longest, line);
    }

    if(max > 0) /* c'era almeno una linea in input */
    printf("%s", longest);
    }

    /* getline: legge e carica in s una linea, ritorna la lunghezza */
    int getline(char s[], int lim)
    {
    int c, i;

    for(i = 0; i < line - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    s = c;

    if(c == '\n')
    {
    s = c;
    ++i;
    }

    s = '\0';

    return i;
    }

    /* copy: copia 'from' in 'to'; assume che 'to' sia sufficientemente ampio */
    void copy(char to[], char from[])
    {
    int i;

    i = 0;

    while((to = from) != '\0')
    ++i;
    }

    I've copied it by hand from the book, the indentation is mine.

    The line with the error is

    for(i = 0; i < line - 1 && (c = getchar()) != EOF && c != '\n'; ++i)

    it should be

    for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)

    Then, of course, you have to change

    main()
    {

    ...
    }

    to

    int main()
    {

    ...

    return 0;
    }

    but this is not a typo...

    I've got also a question: what advantage do one has declaring a function
    with different parameters from the definition like in the program above?

    PS: I've searched for this error in the errata but I haven't found it so
    I've decided to post it here... I hope this is not considered OT.
     
    Lorenzo Villari, Feb 9, 2004
    #1
    1. Advertising

  2. Lorenzo Villari

    Tim Hagan Guest

    Lorenzo Villari wrote:
    >
    > Ahem... I think I've found a typo on my copy of The C Programming
    > Language Second Edition. On section 1.9 (page 32 on my version, the
    > Italian one ) you do find:


    <snip code>

    I find it on page 29 of the English language edition.

    > The line with the error is
    >
    > for(i = 0; i < line - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    >
    > it should be
    >
    > for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)


    You are correct. It should be 'lim' not 'line'.

    > Then, of course, you have to change
    >
    > main()
    > {
    >
    > ...
    > }
    >
    > to
    >
    > int main()
    > {
    >
    > ...
    >
    > return 0;
    > }
    >
    > but this is not a typo...


    Maybe it is. My edition has:

    main()
    {
    ...

    return 0;
    }

    > I've got also a question: what advantage do one has declaring a
    > function with different parameters from the definition like in the
    > program above?


    None. It would be better to use the more descriptive parameter names in
    both places.

    --
    Tim Hagan
     
    Tim Hagan, Feb 9, 2004
    #2
    1. Advertising

  3. Lorenzo Villari wrote:

    > Ahem... I think I've found a typo on my copy of The C Programming Language
    > Second Edition. On section 1.9 (page 32 on my version, the Italian one )
    > you do find:
    >


    <snip>

    > The line with the error is
    >
    > for(i = 0; i < line - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    >
    > it should be
    >
    > for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)



    Yes, the English language version has lim, not line.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Feb 9, 2004
    #3

  4. > > Then, of course, you have to change
    > >
    > > main()
    > > {
    > >
    > > ...
    > > }
    > >
    > > to
    > >
    > > int main()
    > > {
    > >
    > > ...
    > >
    > > return 0;
    > > }
    > >
    > > but this is not a typo...

    >
    > Maybe it is. My edition has:
    >
    > main()
    > {
    > ...
    >
    > return 0;
    > }


    Oh you're right... when I've pasted the program I've reedited it
    and the return statement has disappeared!

    >
    > > I've got also a question: what advantage do one has declaring a
    > > function with different parameters from the definition like in the
    > > program above?

    >
    > None. It would be better to use the more descriptive parameter names in
    > both places.
    >


    Why do you think they used the first form in the book?
     
    Lorenzo Villari, Feb 10, 2004
    #4
  5. Lorenzo Villari

    Tim Hagan Guest

    Lorenzo Villari wrote:
    >


    <snip>

    > > > I've got also a question: what advantage do one has declaring a
    > > > function with different parameters from the definition like in the
    > > > program above?

    > >
    > > None. It would be better to use the more descriptive parameter names
    > > in both places.
    > >

    >
    > Why do you think they used the first form in the book?


    Probably just to demonstrate that they *can* be different. In Section
    1.7, they write: "Parameter names need not agree. Indeed, parameter
    names are optional in a function prototype.... Well-chosen names are
    good documentation, however, so we will often use them." But not always,
    apparently.

    --
    Tim Hagan
     
    Tim Hagan, Feb 10, 2004
    #5
  6. "Lorenzo Villari" <> wrote in message
    news:M0SVb.27168$...
    > Ahem... I think I've found a typo on my copy of The C Programming Language
    > Second Edition. On section 1.9 (page 32 on my version, the Italian one )

    you
    > do find:


    <snip>

    > The line with the error is
    >
    > for(i = 0; i < line - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    >
    > it should be
    >
    > for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)


    Indeed.

    > Then, of course, you have to change
    >
    > main()
    > {
    > ...
    > }
    >
    > to
    >
    > int main()
    > {
    > ...
    > return 0;
    > }


    Indeed. Even better, int main (void).

    > I've got also a question: what advantage do one has declaring a function
    > with different parameters from the definition like in the program above?


    What different parameters? Do you mean lim instead of maxline as the
    parameter name? Formal parameter names are ignored, so you can name them
    whatever you want in the actual definition. And there is no advantage to
    name them differently, as far as I know. But you can leave the formal
    parameter names out of prototypes altogether (for example, to save space)
    and only use them in the actual definition.

    > PS: I've searched for this error in the errata but I haven't found it so
    > I've decided to post it here... I hope this is not considered OT.


    It is perfectly on-topic, you are doing just fine.

    Peter
     
    Peter Pichler, Feb 15, 2004
    #6
    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. Richard Fagen
    Replies:
    0
    Views:
    423
    Richard Fagen
    Aug 28, 2003
  2. Theodore H. Smith

    url typo

    Theodore H. Smith, Jul 29, 2003, in forum: XML
    Replies:
    0
    Views:
    534
    Theodore H. Smith
    Jul 29, 2003
  3. Vijay Kumar R Zanvar

    Typo in n869

    Vijay Kumar R Zanvar, Jun 10, 2004, in forum: C Programming
    Replies:
    1
    Views:
    330
    those who know me have no need of my name
    Jun 10, 2004
  4. Gregor
    Replies:
    0
    Views:
    280
    Gregor
    Oct 17, 2003
  5. Peter Mott

    Docs. for logging module typo

    Peter Mott, Feb 12, 2005, in forum: Python
    Replies:
    0
    Views:
    288
    Peter Mott
    Feb 12, 2005
Loading...

Share This Page