473,394 Members | 1,932 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,394 software developers and data experts.

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 6562
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********************@comcast.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 "obtainAnswerCharacterFromUser()", 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 "interactive 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********************@comcast.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.com, 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/googlegroupsreply/>
May 7 '06 #10
Eric Sosman wrote:
[...]
(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!)


But if that were the case, then any time someone were to invoke UB in
some code, they would also be invoking UB in the group's response, in
effect causing a meta-UB rip in the time-space continuum.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 8 '06 #11

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

Similar topics

21
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...
12
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...
1
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...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
6
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...
11
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...
25
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
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...
22
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...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
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
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,...
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.