By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

K&r typo

P: n/a
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.







Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 14 '05 #2

P: n/a
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 : bi****@eton.powernet.co.uk
"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
Nov 14 '05 #3

P: n/a
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?


Nov 14 '05 #4

P: n/a
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
Nov 14 '05 #5

P: n/a
"Lorenzo Villari" <vl****@tiscali.it> wrote in message
news:M0*******************@twister1.libero.it...
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
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.