473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help terminating a simple while statement

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
6 2949
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
39
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
14
by: Nick Z. | last post by:
I have a file that I want to read a UTF-16 unicode string from. What is the easiest way to accomplish that? Thanks in advance, Nick Z.
43
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
19
by: SP | last post by:
I am learning C and have a question re: malloc(). I wrote simple program which assigns a value to a structure and then prints it as follow: #include <stdio.h> #include <stdlib.h> struct...
8
by: anand devarajan | last post by:
#include"stdio.h" #include<conio.h> #include<math.h> void main() { int row,result; char col; clrscr(); printf("enter the row\n"); scanf("%d",&row);
7
by: jim | last post by:
I need to have 2 simple barcode reader applications finished by midnight tonight. I have never written any barcode reader software and need any help that you may be able to offer. I do not know...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.