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 8708863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:14:05+00:00 2026-06-13T04:14:05+00:00

I am going through this book and have hit some examples that I am

  • 0

I am going through this book and have hit some examples that I am not sure how to test from chapter 1. They have you reading in lines and looking for different characters but I have no idea how to test the code in C that I have made.

For example:

/* K&R2: 1.9, Character Arrays, exercise 1.17

STATEMENT:
write a programme to print all the input lines
longer thans 80 characters. 
*/
<pre>
#include<stdio.h>

#define MAXLINE 1000
#define MAXLENGTH 81

int getline(char [], int max);
void copy(char from[], char to[]);

int main()
{
  int len = 0; /* current line length */
  char line[MAXLINE]; /* current input line */

  while((len = getline(line, MAXLINE)) > 0)
{
  if(len > MAXLENGTH)
printf("LINE-CONTENTS:  %s\n", line);
}

return 0;
}
int getline(char line[], int max)
{
int i = 0; 
int c = 0; 

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

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

 line[i] = '\0';

 return i;
} 

I have no idea how to create a file with varying line lengths to test this on. After doing some research I saw someone try it this way:

[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-17.c
[arch@voodo kr2]$ ./a.out 
like htis
and
this line has more than 80 characters in it so it will get printed on the terminal right
now without any troubles. you can see for yourself
LINE-CONTENTS:  this line has more than 80 characters in it so it will get printed on the
terminal right now without any troubles. you can see for yourself
but this will not  get printed
[arch@voodo kr2]$ 

But I have no idea how he manages it. Any help would be greatly appreciated.

  • 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-06-13T04:14:06+00:00Added an answer on June 13, 2026 at 4:14 am
    for(i = 0; ((c = getchar()) != EOF) && c != '\n' && i < max - 1; ++i) 
    

    This is the line that tells you everything you need to know about your getline() function.

    It will read character by character and store it in the array until:

    1. You don’t press ^D(linux)/^Z(win) on the terminal(^ = control)
    2. You don’t press “enter” key on your keyboard
    3. The number of characters entered shouldn’t be more than max - 1. Otherwise they’ll not be copied. In your example max = 1000 hence only 999 characters are input.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going through some examples in a book I have and I have
Preface: This is not a homework question. I'm going through an algos book in
I'm using VS2010 and I'm going through some examples in my book and I'm
I was going through http://web2py.com/book/default/chapter/02 and found this: >>> print 'number is ' +
While going through the book Scala for the Impatient , I came across this
I was going through Jeffrey Palermo's book and came across this syntax. private void
I am going through this tutorial about PDO and have come to the point
Please help me with this code - I am basically going through a book
This is a pretty strange problem. I have a book that I have been
I have been going through the book Learning WCF trying to get my head

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.