473,834 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar

What does the standard say about getchar()? Do you have to press
return to "send" the char to the program, or is it implementation
defined? I read in a book that on some systems the function returns
after you just typed the char, thus, it has no input buffer.
Is that correct?
May 6 '06 #1
10 6617
john wrote:
What does the standard say about getchar()? Do you have to press
return to "send" the char to the program, or is it implementation
defined? I read in a book that on some systems the function returns
after you just typed the char, thus, it has no input buffer.
Is that correct?


Correct. Whether line buffering is performed by your terminal is
implementation defined and cannot be changed from within standard C.
However, on most systems, you can switch between line-mode and
character-mode by using implementation specific functions. You should
consult your system's documentation for further information.

-- Denis
May 6 '06 #2
john wrote:
What does the standard say about getchar()? Do you have to press
return to "send" the char to the program, or is it implementation
defined? I read in a book that on some systems the function returns
after you just typed the char, thus, it has no input buffer.
Is that correct?


getchar() reads and returns the next character from the
stdin stream, or returns EOF if an I/O error occurs or if there
are no more characters. That's what the Standard says (although
it says so more precisely.)

The stdin stream communicates with some kind of an input
source: keyboard, disk file, socket, joystick, microphone, or
telepathic thingummy. The mechanics of how these various
sources produce their characters are as diverse as the sources
themselves -- but they have nothing to do with stdin per se,
and nothing to do with getchar().

--
Eric Sosman
es*****@acm-dot-org.invalid
May 6 '06 #3
I think you'd better say what operating system and C compiler you have.
I use Bloodshed Dev-C++ on Win32 platform. There after read character
with getchar user has to press enter. If you want just read character
and without enter pressing continue in program, use command getche()
(conio.h library). it has the same use as getchar ;o))

May 6 '06 #4
In article <ae************ ********@comcas t.com>,
Eric Sosman <es*****@acm-dot-org.invalid> wrote:
The stdin stream communicates with some kind of an input
source: keyboard, disk file, socket, joystick, microphone, or
telepathic thingummy.


Say, are nasal demons output only, seperate input and output
channels, or bi-directional? Inquiring DS9000 implementers want to know!
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
May 6 '06 #5
On 2006-05-06, Eric Sosman <es*****@acm-dot-org.invalid> wrote:
john wrote:
What does the standard say about getchar()? Do you have to press
return to "send" the char to the program, or is it implementation
defined? I read in a book that on some systems the function returns
after you just typed the char, thus, it has no input buffer.
Is that correct?
getchar() reads and returns the next character from the
stdin stream, or returns EOF if an I/O error occurs or if there
are no more characters. That's what the Standard says (although
it says so more precisely.)


It also says that stdin and stdout are to be line buffered when
connected to an interactive device, and fully buffered otherwise.

The stdin stream communicates with some kind of an input
source: keyboard, disk file, socket, joystick, microphone, or
telepathic thingummy. The mechanics of how these various
sources produce their characters are as diverse as the sources
themselves -- but they have nothing to do with stdin per se,
and nothing to do with getchar().

May 6 '06 #6
Please provide sufficient context, either by quoting or by describing
the parts you are answering to.

<OP wants to know whether getchar() has always to "wait" for '\n' or
whether there are implementations where getchar() works unbuffered>

DeAtHaLiVe schrieb:
I think you'd better say what operating system and C compiler you have.
I use Bloodshed Dev-C++ on Win32 platform. There after read character
with getchar user has to press enter.
As stated by others, the behaviour of getchar() depends on the
behaviour of stdin. stdin seems to be line-buffered rather than
unbuffered for interactive devices in your case.
If you want just read character
and without enter pressing continue in program, use command getche()
(conio.h library). it has the same use as getchar ;o))


You suggest that the OP uses an implementation specific way of reading
in characters from stdin unbuffered. This may not always be possible
due to external (e.g. hardware) restrictions.

I suggest the use of a macro or function wrapping the implementation
specific part -- this way, you can change the actual way of doing this
"under the hood" without affecting the rest of the program.
In addition, if you have wrapped the input and output parts, you can
even more easily adapt to different implementations . Your programme
itself then calls only "obtainAnswerCh aracterFromUser ()", for example,
and it does not play a role whether this is typed in via keyboard
followed or not followed by hitting the enter key or chosen from a
display via mouse or pen or ...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 6 '06 #7
Jordan Abel schrieb:
On 2006-05-06, Eric Sosman <es*****@acm-dot-org.invalid> wrote:
john wrote:
What does the standard say about getchar()? Do you have to press
return to "send" the char to the program, or is it implementation
defined? I read in a book that on some systems the function returns
after you just typed the char, thus, it has no input buffer.
Is that correct?


getchar() reads and returns the next character from the
stdin stream, or returns EOF if an I/O error occurs or if there
are no more characters. That's what the Standard says (although
it says so more precisely.)


It also says that stdin and stdout are to be line buffered when
connected to an interactive device, and fully buffered otherwise.


No, at least not in the C89 last public draft, the C99 standard and
N1124: There, I have "fully buffered if and only if it is not 100%
clear that we are not connected to an interactive device" (in my own
words) where the introduction states that the implementation defines
what constitutes an "interactiv e device".
This means that we have either line buffered or unbuffered stdin.
The only thing I could not find is whether you can portably affect
this behaviour with a setbuf() or setvbuf() as the very first thing
in main() but I have not searched very hard.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 6 '06 #8
Walter Roberson wrote:
In article <ae************ ********@comcas t.com>,
Eric Sosman <es*****@acm-dot-org.invalid> wrote:

The stdin stream communicates with some kind of an input
source: keyboard, disk file, socket, joystick, microphone, or
telepathic thingummy.

Say, are nasal demons output only, seperate input and output
channels, or bi-directional? Inquiring DS9000 implementers want to know!


It should be obvious that nasal demons are no strangers
to input: `fflush(stdin)' ...

(Long ago, comp.lang.c used to teem with descriptions of possible
undefined behaviors. "Demons will fly from your nose" became a sort
of standard, but I think standardization has stifled creativity and
deprived us of much amusement. My favorite U.B. description spoke of
chocolate pudding oozing from the floppy drive. Ah me, they just don't
write 'em like that any more!)

--
Eric Sosman
es*****@acm-dot-org.invalid
May 6 '06 #9
DeAtHaLiVe wrote:

I think you'd better say what operating system and C compiler you
have. I use Bloodshed Dev-C++ on Win32 platform. There after read
character with getchar user has to press enter. If you want just
read character and without enter pressing continue in program, use
command getche() (conio.h library). it has the same use as getchar


What, if anything, are you talking about? You have failed to
include context (see my sig below), are referencing non-existent
routines (getche()) and include files (conio.h). Any time an
article needs a reference to the OS and compiler it is
automatically off-topic here, where we discuss the portable C
language, as defined by the various standards.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
May 7 '06 #10

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

Similar topics

21
639
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets (data,5,stdin); the_number = atoi (data);
12
4099
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.
1
3725
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++;
5
8763
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
6
5288
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
11
2507
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();
25
5456
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
3
12508
by: mahiapkum | last post by:
Hello, I have a code which uses getchar(). #include<stdio.h> int main() { char ch; ch = getchar(); if(ch == 'Y') { printf("Beautiful World\n");
22
3615
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()) ) ....
0
9646
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
10793
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...
0
10510
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9331
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
7758
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
5627
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...
1
4427
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
2
3978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3081
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.