In exercise 1-16 in The C Programming Language (K & R) it asks for us to: revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.
From what I read here, I get the notion that the main routine is everything inside of main
, is this correct? I posted the code for the longest-line program below for more context, but I am confused my what I am being asked to do not the code. Thanks!
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline( char line[], int maxline );
void copy( char to[], char from[] );
/* 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 )
printf( "%s", longest );
return 0;
}
/* getline: read a line into s, return length */
int getline( char s[], int lim )
{
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] = '';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy( char to[], char from[] )
{
int i;
i = 0;
while ( (to[i] = from[i] ) != '' )
++i;
}
6
Yes, the term main routine typically means the routine (AKA function, method, etc) named main
, which is the entry-point of a C program. From Wikipedia:
The main function serves a special purpose in C programs; the run-time environment calls the main function to begin program execution.
2