Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&r typo

Reply
Thread Tools

K&r typo

 
 
Lorenzo Villari
Guest
Posts: n/a
 
      02-09-2004
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[i] = c;

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

s[i] = '\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[i] = from[i]) != '\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.



















 
Reply With Quote
 
 
 
 
Tim Hagan
Guest
Posts: n/a
 
      02-09-2004
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-09-2004
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
 
Reply With Quote
 
Lorenzo Villari
Guest
Posts: n/a
 
      02-10-2004

> > 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?




 
Reply With Quote
 
Tim Hagan
Guest
Posts: n/a
 
      02-10-2004
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
 
Reply With Quote
 
Peter Pichler
Guest
Posts: n/a
 
      02-15-2004
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typo in advertising mark_digital Digital Photography 5 08-21-2004 03:33 PM
Typo in Canon lens specs? Chris Carlen Digital Photography 3 09-19-2003 09:40 AM
Web Matrix - typo error screwed me up for a bit Richard Fagen ASP .Net 0 08-28-2003 04:27 PM
Re: The Typo Jimchip Computer Support 1 08-13-2003 09:29 PM
url typo Theodore H. Smith XML 0 07-29-2003 06:05 PM



Advertisments