K&R pg. 30 - Why are `getline` and `copy` declared twice?
My question is regarding the program on pg. 29 (Section 1.9, Chapter 1) of The ANSI C Programming Language by Kernighan and Ritchie:
Code:
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline); // FIRST
void copy(char to[], char from[]); // FIRST
/* print the longest input line */
main()
{int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line */
printf("%s", longest);
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[],int lim) // SECOND
{int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[]) // SECOND
{int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
The program makes use of two helper functions, `getline` and `copy`, which are declared twice - once at the beginning of the program and again after `main`, when they are defined. (The same thing can also be seen in an alternative version of the program in Section 1.10.)
What is the purpose of this sort of redeclaration? I'm new to C and this my first reading of the chapter, but it looks like the first declaration is just meant to serve as a sort of documentation for the benefit of the reader, is this correct? When might it be used in practice?
I haven't found anything about redeclarations in my searches (I'm not even sure if redeclaration is the correct word to describe what's going on here), so would be grateful for any insights from the community.