Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:09:14+00:00 2026-05-23T15:09:14+00:00

I copied the following C code from K&R. The code is supposed to print

  • 0

I copied the following C code from K&R. The code is supposed to print a line if it is currently the longest line typed by the user.
This is the code:

 #include <stdio.h>

#define MAXLINE 1000
int max;
char line[MAXLINE];
char longest[MAXLINE];

int getline();
void copy();


int main(){

    int len=0;
    extern int max;
    extern char longest[];

    max = 0;
    while((len = getline()) > 0)
        if (len > max){

            max = len;
            copy();
        }

    if (max > 0){

        printf("%s", longest);
    }
    return 0;
 }


    int getline(){

    int c;
    int i;
    extern char line[];

    for(i = 0; i < MAXLINE-1
        &&(c=getchar())!=EOF&&c!='\n';++i)
            line[i] = c;

    if(c=='\n'){
        line[i]=c;
        ++i;
    }

    line[i] = '\0';
    return i;



}




      void copy(){

      int i;
      extern char line[];
      extern char longest[];

       i=0;

       while((longest[i]=line[i])!='\0')
        ++i;
      }

The problem is that it doesn’t appear to work. Running the code, I can type lines into the console but it doesn’t print the longest line. I would appreciate any help.

EXTRA INFO:
I7m using Win7 with Open Watcom compiler.
The console doesn’t fold, it lets me input characters.
Also, I’m not sure that I am stuck in a loop because if I change my while-loop in main() to

while((len = getline()) > 0)
        if (len > max){
            printf("IT WORKS");
            max = len;
            copy();
        }

with a print command if the length of the line is longer than the current max-length, then “IT WORKS” is printed on screen. So it is definitely counting line lengths.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:09:14+00:00Added an answer on May 23, 2026 at 3:09 pm

    The getline returns 1 higher than expected, so if the user just hits enter, it doesnt exit the loop. Also, add getch() before return 0 in your main function, the console exits/folds after printing the longest line. Many books have the same example so if you want the console to remain open, put getch so the user should strike a key before the program exits. Here, try these codes:

    #include <stdio.h>
    
    #define MAXLINE 1000
    int max;
    char line[4];
    char longest[MAXLINE];
    
    int getline();
    void copy();
    
    
    int main()
    {
        int len=0;
        extern int max;
        extern char longest[];
    
        max = 0;
                /* put -1 to getline so it will return the right length... */ 
        while(((len = getline()) - 1) > 0)
        {
            printf("%d \n",len);
            if (len > max)
            {
    
                max = len;
                copy();
            }
        }
    
        if (max > 0)
        {
    
            printf("%s", longest);
        }
    
                /* Put getch() or getchar() so console will wait for a key to be pressed before exiting */
        getch();
        return 0;
     }
    
    
    int getline()
    {
    
        int c;
        int i;
        extern char line[];
    
        for(i = 0; i < MAXLINE-1 &&(c=getchar())!=EOF&&c!='\n';++i)
        {
            line[i] = c;
        }
    
        if(c=='\n')
        {
            line[i]=c;
            ++i;
        }
    
        line[i] = '\0';
        return i;
    }
    
    
    
    
          void copy(){
    
          int i;
          extern char line[];
          extern char longest[];
    
           i=0;
    
           while((longest[i]=line[i])!='\0')
            ++i;
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I copied this code from an example . I've read it 100 times. Array.prototype.map
I copied the following Ruby code from the Internet and made a few changes
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
So I have the following code which I pretty much copied from here .
copied following code from http://jqueryui.com/demos/resizable/#default <meta charset=utf-8> <style>#resizable { width: 150px; height: 150px; padding:
the following is the code I'm using (copied from msdn) but even when the
I have the following code, copied exactly from a exercise from a PHP book.
I copied the following code from OpenGL's Redbook ( Chapter 4: Display Lists )
I copied some Delphi code from one project to another, and found that it
this is copied from http://www.zenspider.com/ZSS/Products/RubyInline/Readme.html , the home of rubyinline, adding/moding as indicated in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.