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_lin e(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_curr ent = get_current_lin e(current_line, MAXLINE)) 0)
{
if(len_current max_seen)
{
max_seen = len_current;
copy_line(longe st, 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_lin e(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;
} 6 2157
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
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_lin e(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_curr ent = get_current_lin e(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(longe st, 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_lin e(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;
}
On Mar 10, 11:42 am, "santosh" <santosh....@gm ail.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'.
?
arnuld wrote:
On Mar 10, 11:42 am, "santosh" <santosh....@gm ail.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.
On Mar 10, 9:38 pm, "santosh" <santosh....@gm ail.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)
:-)
arnuld wrote:
On Mar 10, 11:42 am, "santosh" <santosh....@gm ail.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 somtimes confused about the difference between a
two-dimensional array and an array of pointers..."
then continues to explain int *b; to be...
|
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 XMLDocument reader in .NET platform.
How should people code & character as an ordinary data character?
I don't mean any platform dependent solution just XML's point of view.
|
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; s >= '0' && s <= '9'; ++i)
|
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:
------------------------------- PROGRAMME --------------------------
/* Section 2.7 type conversions
we are asked to write a function "htoi(an array)" that accomplishes
this:
|
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:
------------------------- PROGRAMME -----------------------------
/* K&R2 section 2.8
Increment and Decrement Operators
| |
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);
|
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 that input array on terminal. (in "main")
3.) we will reverse the array. (calling "reverse" in "main")
4.) we will print that reversed array. (in "main")
|
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 && (c=getchar())!=EOF && c!='\n'; ++i)
s = c;
if (c == '\n') {
|
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>
there are some ambiguous errata, for which I propose solutions. Any
comments are welcome.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |