K&r typo

  • Thread starter Lorenzo Villari
  • Start date
L

Lorenzo Villari

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

Tim Hagan

Lorenzo said:
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.
 
R

Richard Heathfield

Lorenzo said:
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:

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

Lorenzo Villari

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

Tim Hagan

Lorenzo said:
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.
 
P

Peter Pichler

Lorenzo Villari said:
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:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

compile error 30
K&R 1-24 15
K&R Exercise 1-21: entab 10
Input Program Problem 9
How to try a range of hex values in C# code ? 0
1-17 in K&R book 9
K$R xrcise 1-13 (histogram) 4
K&R2 section 1.9 character arrays 6

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top