473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

K&R2 section 1.9 character arrays

this is the example programme from that section. it does not print the
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:
/* K&R2 section 1.9 character arrays

example programme

STATEMENT: to take number of lines as input and print the longest line

*/
#include <stdio.h>

#define MAXLINE 1000 /* MAXIMUM length of input line */

int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);

int main() {

int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */

while((len_current = get_current_line(current_line, MAXLINE)) 0)
{
if(len_current max_seen)
{
max_seen = len_current;
copy_line(longest, current_line);
}
}

if(max_seen 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}
return 0;
}

int get_current_line(char s[], int max_length)
{
int c, i;

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

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

s[i] = '\0';
return i;
}
void copy_line(char to[], char from[])
{
int i;

while((to[i] = from[i]) != '\0')
++i;

}

Mar 10 '07 #1
6 2144
sorry, i forgot to tell:

i am using Arch Linux, gcc 4.1.2 using these options:

"gcc -std=c99 -Wall -Wextra input.c"

thanks

Mar 10 '07 #2
arnuld wrote:
this is the example programme from that section. it does not print the
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:
/* K&R2 section 1.9 character arrays

example programme

STATEMENT: to take number of lines as input and print the longest line

*/
#include <stdio.h>

#define MAXLINE 1000 /* MAXIMUM length of input line */

int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);

int main() {

int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */

while((len_current = get_current_line(current_line, MAXLINE)) 0)
{
if(len_current max_seen)
Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
{
max_seen = len_current;
copy_line(longest, current_line);
}
}

if(max_seen 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}
return 0;
}

int get_current_line(char s[], int max_length)
{
int c, i;

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

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

s[i] = '\0';
return i;
}
void copy_line(char to[], char from[])
{
int i;

while((to[i] = from[i]) != '\0')
Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.
++i;

}
Mar 10 '07 #3
On Mar 10, 11:42 am, "santosh" <santosh....@gmail.comwrote:

Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
[ SNIP ]
Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.
thanks Santosh, it works now.

tell me one thing. in the "copy_line" function:
void copy_line(char to[], char from[])
{
int i;

i = 0;
while((to[i] = from[i]) != '\0')
++i;

}

the loop stops as soon as it encounters '\0'. if loop breaks at this
point, this means '\0' was *not* copied as next element. during
printing, we are using "%s" as argument which accepts an "array" and
an array end sin '\0' but our copied array does not have '\0'.

?

Mar 10 '07 #4

arnuld wrote:
On Mar 10, 11:42 am, "santosh" <santosh....@gmail.comwrote:

Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
[ SNIP ]
Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.

thanks Santosh, it works now.

tell me one thing. in the "copy_line" function:
void copy_line(char to[], char from[])
{
int i;

i = 0;
while((to[i] = from[i]) != '\0')
++i;

}

the loop stops as soon as it encounters '\0'.
Read the construct again.

The test against null character is made _after_ the copy has taken
place. The result of the subexpression is the value of the left
operand of the assignment operator. If it's a null character, (which
is already copied to the destination array), the loop breaks.

Mar 10 '07 #5
On Mar 10, 9:38 pm, "santosh" <santosh....@gmail.comwrote:
Read the construct again.
i did and did not get.
The test against null character is made _after_ the copy has taken
place. The result of the subexpression is the value of the left
operand of the assignment operator. If it's a null character, (which
is already copied to the destination array), the loop breaks.
NOW i got. but i want to tell you one thing, it is........ GREAT.

i mean it looked difficult and alien to me but NOW i see it is so only
from "outside" but i think and feel a language which uses such *terse*
constructs will become my favourite. there is something behind C that
is powerful and has its own culture and following. i say so only after
you explained to me what that sentence is.

sorry for going a little OT and i am impressed (i always thought C is
quite old and ugly and will become a "pain in the ass" BUT my feelings
are changing now)
:-)

Mar 10 '07 #6
arnuld wrote:
On Mar 10, 11:42 am, "santosh" <santosh....@gmail.comwrote:
<snip>
thanks Santosh, it works now.
<snip>
[ ... ] during printing, we are using "%s" as argument which accepts an "array" and
an array end sin '\0' but our copied array does not have '\0'.
The %s specifier expects an argument of type of const char *. It
interprets the contents beginning at the location held in the argument
as a string. Thus only a valid C string must be passed as an argument
to the %s conversion specifier.

A char array need not end in a '\0'. A string must be terminated by
one.

Mar 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
11
by: BenI | last post by:
Hello, I have a XML file using Iso 8859-1 encoding. Every time when I try to put & character as Tag's value like <tag>This is & character</tag> Exception is shown in my application. I use...
7
by: arnuld | last post by:
this is the programme which converts a string of digits into its numeric equivalent, given in section 2.7: /* atoi: convert s to integer */ int atoi(char s) { int i, n; n = 0; for (i = 0;...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
5
by: arnuld | last post by:
it compiles without any trouble but produces "Segmentation Fault" when i try to run it. since i am at chapter 2 so my knowledge of arrays is limited to chapter 1: -------------------------...
4
by: arnuld | last post by:
/* K&R@, 1.5.1 - File Copying */ #include<stdio.h> int main() { int c; while((c = getchar())!= EOF) putchar(c);
16
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print...
2
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 &&...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.