473,795 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic c i/o and EOF

Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end? It orks if i put something like 'q' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {

long nc;

nc = 0;
while (getchar() != 'EOF') {
++nc;
}
printf("%ld\n", nc);

}

</code>

thanks
Dave

Nov 13 '05 #1
28 4605
"Dave" <no*****@noemai l.com> skrev i meddelandet
news:bj******** **@titan.btinte rnet.com...
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end?


No, because when you type -1 it isn't stored as a character, but two
characters '-' and '1'. '-1' however is a single character, and those two
values doesn't match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to '-' and '1'.

--
Tim Cambrant
<tim at cambrant dot com>
Nov 13 '05 #2
On Thu, 11 Sep 2003 15:09:58 GMT, "Tim Cambrant" <ti*@cambrant.c om.net> wrote:
"Dave" <no*****@noemai l.com> skrev i meddelandet
news:bj******* ***@titan.btint ernet.com...
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end?


No, because when you type -1 it isn't stored as a character, but two
characters '-' and '1'. '-1' however is a single character, and those two
values doesn't match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to '-' and '1'.


Or better yet, if your platform supports it, enter the key or key combination
that signals end-of-input-data.

On MSDOS-based systems, this would be the ^Z key combination
On Unix-based systems, this would be what ever combination generates the 'eof'
signal (typically ^D, but check your stty settings to be sure).
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 13 '05 #3
Dave <no*****@noemai l.com> wrote:
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end? Already addressed in the other replies.
It orks if i put something like 'q' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() { int main( void ) {

long nc;

nc = 0;
while (getchar() != 'EOF') { ^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)
++nc;
}
printf("%ld\n", nc);
return 0;}

</code> AFAIK it is implementation defined how to produce an EOF via the
keyboard (e.g. Ctr-Z, Ctrl-C, Ctrl-D, ... ).
thanks
Dave


Regards

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #4
On Thu, 11 Sep 2003, Irrwahn Grausewitz wrote:
Dave <no*****@noemai l.com> wrote:

long nc;

nc = 0;
while (getchar() != 'EOF') {

^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)


It's not "illegal," just implementation-defined. In fact, 'EOF' might
even equal EOF if you're lucky. :-)

--
au***@axis.com


Nov 13 '05 #5
In <ef************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*****@freene t.de> writes:
Dave <no*****@noemai l.com> wrote:
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end?

Already addressed in the other replies.
It orks if i put something like 'q' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {

int main( void ) {

long nc;

nc = 0;
while (getchar() != 'EOF') {

^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)


'EOF' is perfectly legal as a character constant, just not what the OP
needs in his program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
Johan Aurér <au***@axis.com > wrote:
On Thu, 11 Sep 2003, Irrwahn Grausewitz wrote:
Dave <no*****@noemai l.com> wrote:
>
> long nc;
>
> nc = 0;
> while (getchar() != 'EOF') { ^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)


It's not "illegal," just implementation-defined.

Hmph, you're so right.
In fact, 'EOF' might
even equal EOF if you're lucky. :-)


I would call this bad luck, in that it would hide the fact that
one wrote unportable code. :-)

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #7
Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.
Nov 13 '05 #8
In article <bj**********@t itan.btinternet .com>, amanayin wrote:
Do you people know that code is taken from page 18
of The C programming language by kernigan & ritchie.


Yes. It is similar to, but not the same as the code on that
page in that book.

--
Andreas Kähäri
Nov 13 '05 #9
Irrwahn Grausewitz <ir*****@freene t.de> writes:
Dave <no*****@noemai l.com> wrote:
while (getchar() != 'EOF') {

^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)


Not illegal, but its value is implementation-defined. It is
certainly not what the OP wants.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9291
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. # No warranty express or implied for the accuracy, fitness to purpose
9
2242
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script, FILE *in, FILE *out, FILE *err); It returns 0 on success or -1 on fail.
56
4139
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation error. (Not as infrequent as some bugs that have been discussed here in the past, but maybe once in an overnight run of the program when it was configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
14
2515
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in objects. But if they are objects why it´s need this conversion? Aren´t objects (basic types) like Java?
3
2543
by: sefe dery | last post by:
hi ng, i try to create a asp.net 1.0 website on windows server 2003(Servername: ServerX) with iis 6.0. PROBLEM: The user should login with his windows credentials in basic.aspx and automatically redirect to his own files. i have the following file-structure:
13
3037
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format for accessing objects, controls, etc. in VB/Access2003. In particular where will I find explanations of:- Actions, Functions, Methods, Properties - I'm understand the
10
2669
by: trippeer | last post by:
I have the source code to an old BASIC program that a friend of mine would like to run online. I am a beginner at JS, but I think that it would be a good choice for the project. My background is in C/C++ and web development. Any suggestions that might get me off to a good start here? I can provide more information if needed, but I am not sure what would be helpful. The program is 550 lines in what appears to be BASIC and is a calendar...
97
5549
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in the first place. Anyone else heard about this development? The Master
111
5598
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks in advance! -entengk
6
3147
by: Simon Walsh | last post by:
I'm an Electronics student in college and I'm currently working on a project. I was given a circuit diagram for my project, from which I had to design a printed circuit board to be sent off and manufactured. I got my printed circuit board back and populated it with components. On my circuit board, I have a chip holder for a Basic STAMP microcontroller. To those unfamiliar with it, the Basic STAMP is a microcontroller which has an onboard...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.