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

Help terminating a simple while statement

P: n/a
Hello,

I'm working through the K&R book but I can't seem to get over this
one little simple program:

#include <stdio.h>

main()
{
int c;

while((c = getchar()) != EOF)
{
putchar(c);
}
}

.....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?
Thanks,
Gus
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gus Tabares wrote:
Hello,

I'm working through the K&R book but I can't seem to get over this
one little simple program:

#include <stdio.h>

main()
{
int c;

while((c = getchar()) != EOF)
{
putchar(c);
}
}

....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?
Thanks,
Gus


You are expecting the user to enter "end of file" on stdin. Why don't
you simply change the condition to this: while((c = getchar()) != 'q' )
Now the program will echo input until you enter "q"

Cheers,
Jeff

Nov 13 '05 #2

P: n/a
On 2 Jul 2003, Gus Tabares wrote:
Hello,

I'm working through the K&R book but I can't seem to get over this
one little simple program:

#include <stdio.h>

main()
When you don't explicitly put a return type for a function it defaults to
int. So your main() is defined as returning an int. Oddly, that is
correct. If you change it to void you are introducing undefined behaviour.
{
int c;

while((c = getchar()) != EOF)
{
putchar(c);
}
return 0;
}

....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?
Ignoring the missing return statement, your program is perfectly
acceptable. The problem is that the EOF flag is different for every
operating system. On MSDOS I would enter CTRL-Z to signal end of file from
stdin. On my Unix account I have eof configured for CTRL-D (but I can
change that).

You need to go to a newsgroup that is familiar with your operating system.
They should be able to tell you how to signal EOF.

By the way, entering -1 is really entering '-' and then entering '1'.
Completely different from the integer value -1.
Thanks,
Gus


--
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}

Nov 13 '05 #3

P: n/a
Gus Tabares wrote:

Hello,

I'm working through the K&R book but I can't seem to get over this
one little simple program:

#include <stdio.h>

main()
{
int c;

while((c = getchar()) != EOF)
{
putchar(c);
}
}

....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?

Not much. But main is a function of type int. To stay out of trouble in
comp.lang.c you would do it this way..

#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}

Ok, now that we have the program fixed up, what's going on? EOF is a
macro which evaluates to a negative integer, maybe -1. EOF will be
returned at the end of the (stdin) stream. If stdin is your keyboard,
you must tell the implementation about the end of the stream. This is OS
specific but usually Cntrl-Z for Microsoft or Cntrl-D for *nix is
interpreted as EOF from the keyboard.

--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4

P: n/a
On Wed, 2 Jul 2003 16:49:52 UTC, gu*********@verizon.net (Gus Tabares)
wrote:
Hello,

I'm working through the K&R book but I can't seem to get over this
one little simple program:

#include <stdio.h>

main()
{
int c;

while((c = getchar()) != EOF)
{
putchar(c);
}
}

....I am not able to terminate from this loop. In a previous program,
I printed the value of EOF as -1. When I enter -1 the loop still
continues. Is there something I'm missing here?


The EOF confition of stdin is defined by an special keystoke, but it
may differ from OS to OS too.

So use
Strg D
or
Strg Z
Or read the documentation about your system to figure out what
keystroke it iterprets as EOF.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert
Nov 13 '05 #5

P: n/a
In <9o***************************@uranus1.dv-rosenau.de> "Herbert Rosenau" <no****@dv-rosenau.de> writes:
The EOF confition of stdin is defined by an special keystoke, but it
may differ from OS to OS too.

So use
Strg D
or
Strg Z


If you don't have a German keyboard, don't panic! Just use the Control
key instead of Strg ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
On Thu, 3 Jul 2003 11:55:44 UTC, Da*****@cern.ch (Dan Pop) wrote:
In <9o***************************@uranus1.dv-rosenau.de> "Herbert Rosenau" <no****@dv-rosenau.de> writes:
The EOF confition of stdin is defined by an special keystoke, but it
may differ from OS to OS too.

So use
Strg D
or
Strg Z


If you don't have a German keyboard, don't panic! Just use the Control
key instead of Strg ;-)


Grpmf, sorry, but I was now for long, really long time reading english
to get it written in german (somebody says that were translation). So
I had to translate such keys as Del, Ins, Home, Strg.... as well to
make the translations well. As I'd used a short break to read news
instead of sleeping - I was continue working.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.