Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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] = '\0';    
    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] ) != '\0' )
        ++i;
}
share|improve this question
3  
K&R isn't a great book to actually learn C from, largely because of its age –  ratchet freak Apr 7 at 13:08
    
@ratchetfreak I am reading it along with the course a VTC course here vtc.com/products/cprogramming2007.htm, I also read most of the C portion of C how to Program by Dietel last summer. But I like K & R because it is very concise. Do you know of any concise books similar to K&R that are more recent? –  JimmyJackson Apr 7 at 13:16
1  
Personally, I'd recommend C: The Complete Reference by Herbert Schildt although later editions are fairly weighty... –  Robbie Dee Apr 7 at 15:02
    
@RobbieDee Thanks, I looked through the table of contents. The first seven chapters kind of follow K&R which is nice, and I see that there is a lot of useful material not covered in K&R. –  JimmyJackson Apr 7 at 15:14
1  
There is "The C Answer Book" which "Provides solutions to all exercises in Kernighan & Ritchie's new ANSI C book. Ideal for use with K&R in any course on C. Careful study of this answer book will help understand ANSI C and enhance programming skills. Tondo & Gimpel describe each solution and completely format programs to show the logical flow." I've not read it. –  msw Apr 7 at 16:05

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks for confirming this. I was unsure what the question meant by "change the main routine"; I kept wanting to change the functions not the main routine, that is why I want to clarify that, that was not what the question wanted. After, confirming this I thought of away to answer the question, by only changing main while leaving the functions untouched and still in use. –  JimmyJackson Apr 7 at 15:08
    
Sure. It sounds like the challenge was to figure out a solution without changing the subroutines. –  Mike Partridge Apr 7 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.