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