K&r chapter-1; exc-17

T

Trent Buck

Up spake Merrill & Michele:
I use the platform that dare not speak its name in this environment. I
don't change whores in midstream until there's a darn good reason to do so.

Guh. OT, and I nearly postpostscripted this before.

Both GNU Emacs and Vim have NT binaries:
http://www.gnu.org/software/emacs/windows/faq2.html#where-precompiled
http://www.vim.org/download.php#pc

Note that by `platform' I've assumed you meant `operating system'. If
OTOH you meant `IDE', then your claim may have some merit; I've never
used an IDE so I shan't comment.
 
M

Michael Mair

Merrill said:
"Trent Buck" ...
[broken quote level repaired]
Up spake Merrill & Michele:
It's also on my short list to write a prog that counts open and close
>>>braces
I have one of those. It's called emacs.

-t
PS: I expect vi(m) does it, too.

I use the platform that dare not speak its name in this environment.

http://www.gnu.org/software/emacs/windows/
http://www.vim.org/download.php#pc
http://www.textpad.com/
http://www.ultraedit.com/
....

or do yourself a favour and get cygwin:
www.cygwin.com
(You will also want Cygwin/X.
BTW: Then you can also have a look at NEdit (www.nedit.org))

I
don't change whores in midstream until there's a darn good reason to do so.

Please spare us your promiscuity habits as they are OT and
might cause discomfort to sensitive romantics...


-Michael
 
K

Keith Thompson

Merrill & Michele said:
At the risk of incurring wrath of others, I'd like to say the following:
Heute errinert mich an dem an Tschernobyl ueber uns flog. Man kann, am Tage
nach Erntedankfest aus dem Hause heraus. Es graupelt und wir haben einen
Zustand von Minivan_shopping_hysterie.
[...]

Why do you post snippets of German here? If you're expecting anyone
to be impressed by the fact that you're bilingual, we really couldn't
care less. It makes you look like a fool who's far more impressed
with himself than anyone else is.

If you want to post here, write about C in English.
 
M

Michael Mair

Your German ramblings unfortunately are not more congruent or coherent
than your English ones. Apart from that, they are not about C, thus
doubly non-topical.
[...]

Why do you post snippets of German here? If you're expecting anyone
to be impressed by the fact that you're bilingual, we really couldn't
care less. It makes you look like a fool who's far more impressed
with himself than anyone else is.

If you want to post here, write about C in English.

Which reminds me: There is of course also de.comp.lang.c, if our advice
is too hard to understand in English. They won't be interested in the
26th of April, 1986, either.


Cheers
Michael
 
M

Merrill & Michele

Michael Mair said:
"Trent Buck" ...
[broken quote level repaired]
Up spake Merrill & Michele:
It's also on my short list to write a prog that counts open and close
braces
I have one of those. It's called emacs.

-t
PS: I expect vi(m) does it, too.

I use the platform that dare not speak its name in this environment.

http://www.gnu.org/software/emacs/windows/
http://www.vim.org/download.php#pc
http://www.textpad.com/
http://www.ultraedit.com/
...

or do yourself a favour and get cygwin:
www.cygwin.com
(You will also want Cygwin/X.
BTW: Then you can also have a look at NEdit (www.nedit.org))

I
don't change whores in midstream until there's a darn good reason to do
so.

Please spare us your promiscuity habits as they are OT and
might cause discomfort to sensitive romantics...
I'm only half-way there, Michael. The computer I use to talk to the rest of
the world is also the one my wife controls. PLEASE provide protocols on
spouse control if you possess them. Did you honestly not get the pun? MPJ
 
M

Merrill & Michele

Keith Thompson said:
Merrill & Michele said:
At the risk of incurring wrath of others, I'd like to say the following:
Heute errinert mich an dem an Tschernobyl ueber uns flog. Man kann, am Tage
nach Erntedankfest aus dem Hause heraus. Es graupelt und wir haben einen
Zustand von Minivan_shopping_hysterie.
[...]

Why do you post snippets of German here? If you're expecting anyone
to be impressed by the fact that you're bilingual, we really couldn't
care less. It makes you look like a fool who's far more impressed
with himself than anyone else is.

If you want to post here, write about C in English.

Mr. Thompson,

I think you snipped me unfairly. My question was EXACTLY about C and ON T.
After my question, I made a bunch of dashes and said a few words about who
cares what:

Thanks Jens. Can I ask what your compiler said about that first error? I
think it's an important modification from K&R days to #define MAXLINE a good
deal larger. MPJ
------
At the risk ....

I am not bilingual and have never walked through a Tor, not even the
Brandenburg. Switching languages sometimes stops the hemorrhaging when I
unwittingly step on a land mine. I shut up when you tell me to, even when
people keep piling on a tackled man. So when you say '[in clc] write in
English', do you assert a leadership role or do you heckle from the cheap
seats? MPJ
 
M

Merrill & Michele

Jens Toerring
[anything having to do with Germany snipped]

I think this qualifies as an answer to the OP. I'm confused about several
aspects of this, and I think the development is inappropriate with the
pointers in §1.*. The hardest part of this exercise, given the code in the
text, is having a plain text file to test on. I provide such a link at the
end. My brother's English is as beautiful as his family and the game of
baseball. In case you're a couple steps behind me, you type the resulting
executable on the command line and then <luke.txt , allowing whitespace and
having the latter file in the same directory. MPJ

source

#include <stdio.h>
#define MAXLINE 5000
#define TJA 80

int getline(char *line, int maxline);
/* void copy(char *to, char *from); */

int main(void){

int len = 0;
char line[MAXLINE];


while ((len = getline(line, MAXLINE)) > 0){
if (len > TJA) {
printf("%slength was %d \n\n", line, len);
len = 0;
}
}


return 0;
}

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') {
s = c;
++ i;
}
s = '\0';

return i;
}
/* unnecessary
void copy(char *to, char *from) {

int i;

i = 0;
while ((to = from) != '\0') ++ i;
}
*/

end source

http://home.comcast.net/~beckjensen/luke.txt
 
M

Michael Mair

Merrill said:
Merrill & Michele wrote: [snip: discussion about changing to a good editor for programming]
I
don't change whores in midstream until there's a darn good reason to do
so.

Please spare us your promiscuity habits as they are OT and
might cause discomfort to sensitive romantics...

I'm only half-way there, Michael. The computer I use to talk to the rest of
the world is also the one my wife controls. PLEASE provide protocols on
spouse control if you possess them. Did you honestly not get the pun? MPJ

I did get it. It was not a good pun in the sense of me cringing (*)
nor in the sense of me laughing out loud. I just found it
inappropriate.
Apart from that: Spouse _control_ is a Bad Idea.
And no: I am not entirely without humour.


Cheers
Michael

(*): A nice "definition" of pun can be found here:
http://www.sluggy.com/daily.php?date=041117
 
M

Michael Mair

Merrill said:
Jens Toerring


[anything having to do with Germany snipped]

I think this qualifies as an answer to the OP. I'm confused about several
aspects of this, and I think the development is inappropriate with the
pointers in §1.*. The hardest part of this exercise, given the code in the
text, is having a plain text file to test on. I provide such a link at the
end. My brother's English is as beautiful as his family and the game of
baseball. In case you're a couple steps behind me, you type the resulting
executable on the command line and then <luke.txt , allowing whitespace and
having the latter file in the same directory. MPJ

source

#include <stdio.h>
#define MAXLINE 5000
#define TJA 80

int getline(char *line, int maxline);
/* void copy(char *to, char *from); */

int main(void){

int len = 0;
char line[MAXLINE];


while ((len = getline(line, MAXLINE)) > 0){
if (len > TJA) {
printf("%slength was %d \n\n", line, len);
len = 0;
}
}


return 0;
}

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') {
s = c;
++ i;
}
s = '\0';

I would replace this by
for (i = 0, c = 0; i < lim - 1 && c != '\n' && (c = getchar()) !=
EOF; ++ i)
s = c;
s = '\0';
So, you avoid the problem of c being uninitialised and potentially
having the value '\n' for lim <= 1 and draw the comparison into the
loop condition instead of handling this case separately.
If this is too convoluted for your liking, at least initialise c.
return i;
}
/* unnecessary
void copy(char *to, char *from) {

int i;

i = 0;
while ((to = from) != '\0') ++ i;
}
*/

end source

http://home.comcast.net/~beckjensen/luke.txt


You evade the problem of long lines. Reset MAXLINE to 10 and
TJA to 5 and have a look at the result. If you ever encounter
a line/paragraph with enough characters, your code _will_
break or not do what you expect it to do.
Why settle for a solution like that?


Cheers
Michael
 
M

Merrill & Michele

"Michael Mair" :
[heavily snipped and reorganized]
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++ i) s =
c;
if (c == '\n') {
s = c;
++ i;
}
s = '\0';

I would replace this by
for (i = 0, c = 0; i < lim - 1 && c != '\n' && (c = getchar()) !=
EOF; ++ i)
s = c;
s = '\0';

So, you avoid the problem of c being uninitialised and potentially
having the value '\n' for lim <= 1 and draw the comparison into the
loop condition instead of handling this case separately.
If this is too convoluted for your liking, at least initialise c.

I never thought about that problem. It's not a question of MY palate. I
think people who look at c get discouraged because the learning curve is so
steep. I believe this code addresses your issue and is transparent:

#include <stdio.h>
#define MAXLINE 20000
#define THRESHOLD 80

int getline(char line[], int maxline);

int main(void){

int len = 0;
char line[MAXLINE];

while ((len = getline(line, MAXLINE)) > 0){
if (len > THRESHOLD) {
printf("%slength was %d \n\n", line, len);
len = 0;
}
}

return 0;
}

int getline(char s[], int lim) {

int c, i;

c = 0;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++ i) s =
c;
if (c == '\n') {
s = c;
++ i;
}
s = '\0';

return i;
}

end source
plain text appropriate for a test:
http://home.comcast.net/~beckjensen/luke.txt
You evade the problem of long lines. Reset MAXLINE to 10 and
[THRESHOLD] to 5 and have a look at the result. If you ever encounter
a line/paragraph with enough characters, your code _will_
break or not do what you expect it to do.
Why settle for a solution like that?
http://home.comcast.net/~beckjensen/cstuff5.htm
The result is ugly, as this screen dump shows, but I'm not quite sure what
the source of said ugliness is. I know there's nothing in the Standard
about this question, so I'm opening myself up again to getting clobbered,
but do you have a rule of thumb as to how many characters fit width-wise on
95% of the monitors out there? And am I correct to think that the reason
the statement that outputs the length seems to be one shy is that I stopped
iterated in the called function at (lim - 1) = (MAXLINE - 1) ? MPJ
 
C

CBFalconer

.... snip ...

If you get further in K&R you will find that the whole function can
be written as

void copy( char *to, const char *from )
{
while ( *to = *from )
;
}

That will efficiently copy empty strings, but cause a considerable
delay for other entities.
 
M

Merrill & Michele

"Trent Buck" so.

Guh. OT, and I nearly postpostscripted this before.

Both GNU Emacs and Vim have NT binaries:
http://www.gnu.org/software/emacs/windows/faq2.html#where-precompiled
http://www.vim.org/download.php#pc

Note that by `platform' I've assumed you meant `operating system'. If
OTOH you meant `IDE', then your claim may have some merit; I've never
used an IDE so I shan't comment.
work.

I see now. I had mixed success with such endeavors today. This is a topic
where I'm not pretending to be stupid nor am I falsely accused of being so.
When I see the unix $ prompt, I think it really needs to be a ~$ . MPJ
 
K

Keith Thompson

Merrill & Michele said:
Keith Thompson said:
Merrill & Michele said:
At the risk of incurring wrath of others, I'd like to say the
following: Heute errinert mich an dem an Tschernobyl ueber uns
flog. Man kann, am Tage nach Erntedankfest aus dem Hause heraus.
Es graupelt und wir haben einen Zustand von
Minivan_shopping_hysterie.
[...]

Why do you post snippets of German here? If you're expecting anyone
to be impressed by the fact that you're bilingual, we really couldn't
care less. It makes you look like a fool who's far more impressed
with himself than anyone else is.

If you want to post here, write about C in English.

Mr. Thompson,

I think you snipped me unfairly. My question was EXACTLY about C and ON T.

I did not snip you unfairly. I snipped the majority of your article
because I wasn't commenting on it; I quoted only what was relevant to
my followup. The bulk of your article may well have been perfectly
topical.

I have nothing more to add.
 
R

Richard Bos

We can see what system you use (just have a look at the mesage
headers yourself)

No, you can't; you can see what system he posts from, but that is not
necessarily the one he programs for. For example, there are quite a few
C programmers who work with embedded devices, but I've never seen anyone
post from a vending machine :)
and there are perfectly well working implementations for both these
editors for that OS

FSVO "perfectly well working" that assume you consider either to be a
good editor, of course. FWIW, many normal M$-Windows editors can count
and match braces, too.

Richard
 
D

Dan Pop

I posted something downthread that I thought worked. Did I stumble into
non-ANSI/ISO or undefined behavior?

Into something standard C doesn't allow. I'm too lazy to check whether
it's a syntax rule or constraint violation, but it doesn't make any
practical difference, anyway.
No, I just blew the close-brace.

Which, from the compiler's point of view, is undistinguishable from
defining getline() inside main().
It's also on my short list to write a prog
that counts open and close braces, but I've only got 32 hours in a day. MPJ

As I said in my previous message, a better bracing style would have made
your mistake glaringly obvious without having to count anything at all.
Compare:

int main()
{
int foo;
...
return 0;
}

int getline()
{

and

int main()
{
int foo;
...
return 0;

int getline()
{

The misalignment between the return statement and the line starting the
definition of the new function is a strong visual clue that something is
wrong: there *must* be a missing closing brace in between.

Dan
 
M

Merrill & Michele

"Dan Pop"

Into something standard C doesn't allow. I'm too lazy to check whether
it's a syntax rule or constraint violation, but it doesn't make any
practical difference, anyway.


Which, from the compiler's point of view, is undistinguishable from
defining getline() inside main().
MPJ

As I said in my previous message, a better bracing style would have made
your mistake glaringly obvious without having to count anything at all.
Compare:

int main()
{
int foo;
...
return 0;
}

int getline()
{

and

int main()
{
int foo;
...
return 0;

int getline()
{

The misalignment between the return statement and the line starting the
definition of the new function is a strong visual clue that something is
wrong: there *must* be a missing closing brace in between.

Guilty on all counts. I'm going to see if cygwin makes my development less
sloppy. I'm having a bit of a rough go with these $ prompts. Mr. Mair gave
me some tips that might do the trick, though. MPJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

K$R xrcise 1-13 (histogram) 4
K&R 1-24 15
1-17 in K&R book 9
K&R 5-1 9
Beginner at c 0
K&R xrcise 1-13 6
K&R Exercise 1-21: entab 10
C pipe 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top