Help terminating a simple while statement

G

Gus Tabares

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
 
J

Jeff

Gus said:
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
 
D

Darrell Grainger

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.
 
J

Joe Wright

Gus said:
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.
 
H

Herbert Rosenau

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
 
D

Dan Pop

In said:
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
 
H

Herbert Rosenau

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top