473,900 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar function and EOF problem..


Can someone help me out with this one?

The following program should read characters till it met the EOF (-1)
on my pc.
I'm running linux and using the gcc compiler version 3.4.5.
But it doesn't.
Am I doing something wrong here?.

=============== =============== =============== =

#include <stdio.h>

int main(void)
char c;

printf("EOF has value: %d\n", EOF);

while((c = getchar()) != EOF)

return 0;

=============== =============== =============== ==

Mar 10 '06
13 12432
broeisi wrote:
I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.
See the .sig below for important Google information.
I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.

You didn't enter an integer of value -1. You enter two characters, a
minus sign followed by the character 1. Those will be read separately
via your getchar() calls.

What you need is a way to signal EOF from the keyboard. That is
implementation-specific (if possible at all). Some common solutions are
control Z for DOS and Windows machines, or control D for UNIX.

Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
Mar 11 '06 #11
"A. Sinan Unur" <1u**@llenroc.u de.invalid> writes:
From keyboard input, different shells have different conventions. On
bash, for example, entering a CTRL-D will signal end of input from the
console, whereas if one using cmd.exe on Windows, CTRL-Z would be used.

For Unix-like systems, this is determined by a tty setting, not by the

Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 11 '06 #12
"Albert" <al************ *****@gmail.com > writes:
It looks like you want the program to output the value of EOF.
Try putting the printf statement after the while loop, but before the
return statement.

That makes no sense without context. Please read
<http://cfaj.freeshell. org/google/> to learn why you should provide
context when you post a followup, and how to do so despite Google's
determined efforts to make you look like a fool. (I'm flaming Google,
not you.)

Sadly, it makes no more sense with context. Here's the program
from the original post (I've deleted blank lines to save space):

#include <stdio.h>
int main(void)
char c;
printf("EOF has value: %d\n", EOF);
while((c = getchar()) != EOF)
return 0;

The program is intended to print the value of EOF, and then copy stdin
to stdout. If you had tried it, you would have seen that it does
print the value of EOF.

Moving the printf after the while loop will accomplish nothing, and
has nothing to do with the code's actual problem. In fact, due to the
bug we've been discussing, it could prevent the printf() call from
ever being executed.

Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 11 '06 #13
On 10 Mar 2006 13:11:42 -0800, "broeisi" <br*******@gmai l.com> wrote:
I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.

While the EOF macro may be defined as -1 on your system, the input
characters -1 do not represent end of file. They are simply the '-'
character followed by the '1' character. Also note that EOF is not a
character as such but the (status) value returned by the input
functions to indicate an unusual condition (incomplete or missing
input), such as error or end of file. As a matter of convention,
Windows systems use control-Z as the input character which causes the
input function to return EOF and Unix systems use control-D. You will
need to review your system specific documentation to determine which
value you should use.
Remove del for email
Mar 12 '06 #14

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

Similar topics

by: Cam | last post by:
Hi everyone, Before I answer to a (hopefully) helpful reply to this post, I have been rapped over the knuckles for 'top-posting' and I do not wish to be a learner poster who observes poor netiquette as I am a firm believer in consideration .... to this end, could somebody please tell me how I reply to a message without top-posting? Do I need to have my original post selected when I hit the 'reply' button? Here's my question (and...
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may have missed it). I was wondering wether it was just a 'common' behaviour of most systems, and wether the hit of the ENTER key was mandatory. Thanks for having read me.
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if (!isspace(input)) j++;
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1 #define OUT 0 int word_count();
by: Michael Brennan | last post by:
If a call to getchar() returns EOF, is the behavior defined if another getchar() call is made? Will it return EOF again? The reason I'm asking: if I have a function that uses getchar() repeatedly, and it returns EOF after some bytes, do I need the function to handle the EOF right there, or, can I just wait until the next time my function executes, and handle it there, if EOF will be returned by the first call to getchar().
by: MargNat | last post by:
Hi, I've wrote a simple c++ program without graphic interface; Now, if I build it with Visual Studio function getchar() works in a way; if I build it with Dev-C++ function getchar() works in another way. Why? Then, another question: Is there a specific function to exit program pressing any button?
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, because sign-extension of a variable of type char on widening to integer is implementation-defined" while( EOF != (ch = getchar()) ) ....
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...
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,...
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...
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...
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...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.