By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

K&R2 section 1.9 character arrays

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.