[color=blue][color=green][color=darkred]
> >>>Merrill & Michele wrote:
> >>>It's very difficult to do an exercise with elementary tools. It took[/color][/color][/color]
me[color=blue][color=green][color=darkred]
> >>>about fifteen minutes to get exercise 1-7:
> >>>#include <stdio.h>
> >>>
> >>>int main(int orange, char **apple)
> >>>{
> >>>int c;
> >>>c=-5;
> >>>while(c != EOF )
> >>>{
> >>> c++;
> >>>}
> >>>printf("%d\t",c);
> >>>printf("%d\t",EOF);
> >>>return (0);
> >>>}
> >>>Since my printf's agree that window's EOF is Hamilton's (ijk)^2, I[/color][/color][/color]
think[color=blue][color=green][color=darkred]
> >>>it's right.[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >>Dan wrote:
> >>Nope, it isn't. Your while loop will eventually cause integer overflow,
> >>which is undefined behaviour in C. Furthermore, it is entirely[/color][/color][/color]
irrelevant[color=blue][color=green][color=darkred]
> >>to your problem, whose solution is as simple as:
> >>
> >> #include <stdio.h>
> >>
> >> int main()
> >> {
> >> printf("%d\n", EOF);
> >> return 0;
> >> }[/color][/color][/color]
[color=blue][color=green]
> > Thanks all for replies. Mr. Pop is correct that his shortened version[/color][/color]
of my[color=blue][color=green]
> > prog does indeed yield -1, but I liked the idea of trying to find the
> > appropriate answer with a while{}. Would Mr. Pop have the same[/color][/color]
criticism if[color=blue][color=green]
> > one began the loop with Mr. Barts' (I know I got your 's' this time, but[/color][/color]
did[color=blue][color=green]
> > I get the possessive correct?) INT_MIN suggestion, incremented in the[/color][/color]
usual[color=blue][color=green]
> > fashion, and it were stipulated that an EOF had indeed been #defined as[/color][/color]
an[color=blue][color=green]
> > int?[/color][/color]
[color=blue]
> "Michael Mair" wrote
> He would probably point out that your way is pointless and that you
> should start engaging the gray stuff between your ears... However,
> that is not mine to say.
> If you would bother to read _and_ try to understand everything he says,
> you would find out that the problem with the signed integer overflow
> then still would not be resolved.
> As EOF is a constant negative integer, you can start at INT_MIN and
> stop at 0. If you do not want to do so, you have to make sure that c
> is not increased when it has the value INT_MAX as this leads to
> undefined behaviour.[/color]
Mr. Pop does not require your input to disparage. You are incorrect that I
do not read and try to understand. You are further incorrect to claim K&R2
#defines EOF as a negative int. In the chapter I just read, he says it
can't be an int that could otherwise be a char. Demnach, EOF could be
between CHAR_MAX and INT_MAX. Furthermore, I can't remember if I have a
logical OR yet. Reflecting on it, I could use a for and increment from
INT_MIN to INT_MAX and do just fine, but this does not follow the
development.
[color=blue]
> Once more: Give us the text -- not everyone has a copy right
> next to his hand...[/color]
Schade.
[color=blue][color=green][color=darkred]
> >>>Exercise 1-6 is giving me dyspepsia. This compiles and links but
> >>> does not behave according to my wishes:
> >>>#include <stdio.h>
> >>>
> >>>int main(int orange, char **mango)
> >>>{
> >>>int c;
> >>>c=(getchar() != EOF);
> >>>printf("%d\t",c);
> >>>return (0);
> >>>}[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >>>I'm looking for a 0 or 1 when I peck randomly at the keyboard, and am[/color][/color][/color]
a[color=blue][color=green][color=darkred]
> >>>my wit's end, as I also seem to be unable to debug.[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >>Dan wrote:
>>>Your program is almost correct. Replace \t by \n and it is fully[/color][/color][/color]
correct.[color=blue][color=green][color=darkred]
> >>It would have been even better if you used a loop, with getchar() != EOF
> >>evaluating to 0 as the exit condition, but it works even in your[/color][/color][/color]
version,[color=blue][color=green][color=darkred]
> >>if you understand that stdin, when connected to the terminal, is line
> >>buffered. So, nothing will happen until you press the Return/Enter key.
> >>Your platform may require it even after the eof key. After that, your
> >>getchar() call will return the code of the first character you have[/color][/color][/color]
typed[color=blue][color=green][color=darkred]
> >>or EOF if you have typed the corresponding character for your platform
> >>(CTRL-Z for DOS/Windows, CTRL-D for Unix). So, you can expect a 1 as
> >>output most of the time, unless the first character you have typed was
> >>the one generating an end of file condition.[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >>This program is much easier to work with if you use a loop and redirect
> >>stdin from a file:[/color][/color][/color]
[color=blue][color=green]
> > MPJ wrote:
> > I too have other means of getting data to a prog and avoid the keyboard
> > during runtime like the plague, but the point of this exercise is to[/color][/color]
force[color=blue][color=green]
> > us into this straightjacket.[/color][/color]
[color=blue]
> "Michael Mair" wrote
> It is not.[/color]
It's amazing to announce the intent of an exercise of a book that you don't
have in hand.
[color=blue][color=green][color=darkred]
> > > Dan wrote
> >> fangorn:~/tmp 146> cat input
> >> 12345
> >> fangorn:~/tmp 147> cat test.c
> >> #include <stdio.h>
> >>
> >> int main()
> >> {
> >> int cond;
> >>
> >> while ((cond = getchar() != EOF) == 1) printf("%d ", cond);
> >> printf("%d\n", cond);
> >> return 0;
> >> }
> >> fangorn:~/tmp 148> gcc test.c
> >> fangorn:~/tmp 149> ./a.out <input
> >> 1 1 1 1 1 1 0[/color]
> >
> > Among other things, I don't know whether your 'cat's are like my[/color][/color]
'orange's.[color=blue][color=green]
> > In short, I've never touched Unix.[/color]
>
> "Michael Mair" wrote
> cat <file> essentially reads the file <file> and outputs it to stdout.
> The line "./a.out <input" executes the program "./a.out" and redirects
> "input" to stdin for this program.[/color]
Good to know.
[color=blue][color=green][color=darkred]
> > > Dan wrote:
> >>The input file contains 6 characters: the five visible ones (12345)
> >>and the newline character. For each of them, the expression
> >>getchar() != EOF evaluates to 1 and a 1 is displayed in the program
> >>output. The seventh getchar() call generates and end of file condition
> >>on the stream and it returns EOF. At that point, the expression
> >>getchar() != EOF evaluates to 0 and the while loop is terminated.
> >>The corresponding value of the expression is displayed, followed by
> >>the mandatory newline character.[/color]
> >
> >
> > The following sentence immediately precedes ex. 1-6: "This has the[/color][/color]
effect[color=blue][color=green]
> > of setting c to 0 or 1, depending on whether or not the call of getchar
> > encountered EOF." The 'this' in reference is exactly the first line of[/color][/color]
my[color=blue][color=green]
> > code after declarations. I'm still not getting results. I think I[/color][/color]
remember[color=blue][color=green]
> > Heathfield made some remark about either putchar or getchar, but I can't
> > crack open that book, currently five feet from my knee, as I am going
> > through K&R sequentially. MPJ
> > Output:
> >
http://home.comcast.net/~beckjensen/cstuff4.htm[/color][/color]
[color=blue]
> "Michael Mair" wrote
> To me, it seems that your main trouble is your programming environment.
> Perhaps it would be better to use more basic tools and an environment
> you can understand more easily.
> This has been pointed out to you somewhere else, too.
>
> Apart from that, if you follow the steps Dan pointed out, everything
> should work fine.
> Note that you get stuff from stdin via _any_ of the standard C input
> functions only after you hit the return key.[/color]
My programming environment is definitely austere. I haven't found an
instance of where it has not performed properly given ANSI-compliant C. My
question then is, does the following code, when executed, take keystrokes
and put either a zero or a one as output depending on whether the keystroke
is, respectively, EOF or anything else?
#include <stdio.h>
int main(int orange, char **mango)
{
int c;
c=(getchar() != EOF);
printf("%d\n",c);
return (0);
}
I'll address some form of upgrade after I plough through K&R. MPJ