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

  • Home
  • SEARCH
  • 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 578615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:16:47+00:00 2026-05-13T14:16:47+00:00

I bought a C book called The C (ANSI C) PROGRAMMING LANGUAGE to try

  • 0

I bought a C book called “The C (ANSI C) PROGRAMMING LANGUAGE” to try and teach myself, well C. Anyhow, the book includes a lot of examples and practices to follow across the chapters, which is nice.

Anyhow, the code below is my answer to the books “count the longest line type of program”, the authors are using a for-loop in the function getLine(char s[], int lim). Which allows for a proper display of the string line inside the main() function. However using while won’t work – for a reason that is for me unknown, perhaps someone might shed a light on the situation to what my error is.

EDIT: To summarize the above. printf("%s\n", line); won’t display anything.

Thankful for any help.

#include <stdio.h>
#define MAXLINE 1024

getLine(char s[], int lim) {
    int c, i = 0;

    while((c = getchar()) != EOF && c != '\n' && i < lim) {
        s[++i] = c;
    }

    if(c == '\n' && i != 0) {
        s[++i] = c;
        s[++i] = '\0';
    }    
    return i;
}  

main(void) {
    int max = 0, len;
    char line[MAXLINE], longest[MAXLINE];

    while((len = getLine(line,MAXLINE)) > 0) {
        if(len > max) {
            max = len;
            printf("%s\n", line);
        }
    }
    return 0;
}
  • 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-13T14:16:47+00:00Added an answer on May 13, 2026 at 2:16 pm

    You have a number of serious bugs. Here’s the ones I found and how to fix them.

    change your code to postincrement i to avoid leaving the first array member uninitialised, and to avoid double printing the final character:

    s[++i] = c;
    ...
    s[++i] = c;
    s[++i] = '\0';
    

    to

    s[i++] = c;
    ...
    // s[++i] = c; see below
    ...
    s[i++] = '\0'; 
    

    and fix your EOF bug:

    if(c == '\n' && i != 0) {
        s[++i] = c;
        s[++i] = '\0';
    }
    

    to

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

    Theory

    When writing programs that deal with strings, arrays or other vector-type structures it is vitally important that you check the logic of your program. You should do this by hand, and run a few sample cases through it, providing sample inputs to your program and thinking out what happens.

    The cases you need to run through it are:

    • a couple general cases
    • all the edge cases

    In this case, your edge cases are:

    • first character ever is EOF
    • first character is ‘x’, second character ever is EOF
    • first character is ‘\n’, second character is EOF
    • first character is ‘x’, second character is ‘\n’, third character is EOF

    • a line has equal to lim characters

    • a line has one less than lim characters
    • a line has one more than lim characters

    Sample edge case

    first character is ‘x’, second character is ‘\n’, third character is EOF

    getLine(line[MAXLINE],MAXLINE])
    (s := line[MAXLINE] = '!!!!!!!!!!!!!!!!!!!!!!!!...'
    c := undef, i := 0
    while(...)
    c := 'x'
    i := 1
    s[1] := 'x' => s == '!x!!!!...' <- first bug found
    while(...)
    c := '\n'
    end of while(...)
    if (...)
    (c== '\n' (T) && i != 0 (T)) = T
    i := i + 1 = 2
    s[2] = '\n' => s == '!x\n!!!!'
    i := i + 1 = 3
    s[3] = '\0' => s == '!x\n\0!!!' <- good, it's terminated
    return i = 3
    (len = i = 3) > 0) = T (the while eval)
    if (...)
    len (i = 3) > max = F
    max = 3 <- does this make sense?  is '!x\n' a line 3 chars long?  perhaps.  why did we keep the '\n' character? this is likely to be another bug.
    printf("%s\n", line) <- oh, we are adding ANOTHER \n character?  it was definitely a bug.
    outputs "!x\n\n\0" <- oh, I expected it to print "x\n".  we know why it didn't.
    while(...)
    getLine(...)
    (s := line[MAXLINE] = '!x\n\0!!!!!!!!!!!!!!!!!!!...' ; <- oh, that's fun.
    c := undef, i := 0
    while(...)
    c := EOF
    while terminates without executing body
    (c == '\n' && i != 0) = F
    if body not executed
    return i = 0
    (len = i = 0 > 0) = F
    while terminates
    program stops.
    

    So you see this simple process, that can be done in your head or (preferably) on paper, can show you in a matter of minutes whether your program will work or not.

    By following through the other edge cases and a couple general cases you will discover the other problems in your program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning how to develop on the iPhone, I bought a book called Beginning
I've recently bought myself a new cellphone, running Windows Mobile 6.1 Professional. And of
I bought a book on Amazon which was meant to prepare me for 70-536
i've bought a book learning the java SE 6 platform. i wonder what the
In my feeble attempt to learn JavaScript, I bought a book which teaches you
In order to learn more about Spring.NET, I bought the book Spring in Action,
Just bought a 2.4GHz Intel Core 2 Duo iMac with 2GB of memory and
I bought a new Vista PC recently but was having lots of problems getting
We recently bought a new rack and set of servers for it, we want
I recently bought and read a box set of books on security ( Building

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.