Avoid having to press Enter

J

Johan Svedberg

Hi!

I read somewhere that it is impossible to write a program in ANSI-C
which only reads one char from the keyboard without having to press
Enter (i.e. [y/n]-questions). Is this true?
 
J

Joona I Palaste

Johan Svedberg said:
I read somewhere that it is impossible to write a program in ANSI-C
which only reads one char from the keyboard without having to press
Enter (i.e. [y/n]-questions). Is this true?

That's only more-or-less true. You see, ANSI-C doesn't read anything
from the keyboard at all. Your console does. It then feeds the data to
ANSI-C via a text stream. ANSI-C can read whatever it wants from this
stream, whenever it wants, regardless of whether you press Return or
not.
The thing is, however, your console usually doesn't put the data into
the stream in the first place, until you press Return. Therefore, until
you press Return, ANSI-C is blissfully ignorant to the existence of the
data at all.
Simply switch your console from "cooked" to "raw" mode and you're all
set. How do you switch it? That's an OS question, I'm afraid, not an
ANSI-C one.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo
 
P

pete

Joona said:
Johan Svedberg said:
I read somewhere that it is impossible to write a program in ANSI-C
which only reads one char from the keyboard without having to press
Enter (i.e. [y/n]-questions). Is this true?

That's only more-or-less true. You see, ANSI-C doesn't read anything
from the keyboard at all. Your console does.

I'm looking at my computer, my monitor and my keyboard.
I'm under the impression that my keyboard is the file
associated with the standard input stream and that my monitor
is the file associated with the standard output stream
on my system.
What's my console ?
 
J

Joona I Palaste

pete said:
Joona said:
Johan Svedberg said:
I read somewhere that it is impossible to write a program in ANSI-C
which only reads one char from the keyboard without having to press
Enter (i.e. [y/n]-questions). Is this true?

That's only more-or-less true. You see, ANSI-C doesn't read anything
from the keyboard at all. Your console does.
I'm looking at my computer, my monitor and my keyboard.
I'm under the impression that my keyboard is the file
associated with the standard input stream and that my monitor
is the file associated with the standard output stream
on my system.
What's my console ?

Your console is the program or other software component responsible
for reading from your keyboard and writing to your monitor. ANSI-C
does not provide hardware APIs to the physical devices.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
 
P

pete

Joona said:
Your console is the program or other software component responsible
for reading from your keyboard and writing to your monitor. ANSI-C
does not provide hardware APIs to the physical devices.

I'm trying to understand how consoles
fit into the C standard's description of the world.
There's the computer and there are files.
The computer has an operating system (assuming not freestanding).
The flow of information between the computer and a file,
is a stream.
I guess that the console is part of the operating system,
and that the stream flows through the console, to the file.
 
J

Joona I Palaste

I'm trying to understand how consoles
fit into the C standard's description of the world.
There's the computer and there are files.
The computer has an operating system (assuming not freestanding).
The flow of information between the computer and a file,
is a stream.
I guess that the console is part of the operating system,
and that the stream flows through the console, to the file.

Consoles are part of the operating system, yes. But the only come into
play when you are using the keyboard and the screen. There the console
goes between the actual physical devices and the streams. When using
files, the operating system connects the file directly to the streams.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 
P

pete

Joona said:
Consoles are part of the operating system, yes. But the only come into
play when you are using the keyboard and the screen. There the console
goes between the actual physical devices and the streams. When using
files, the operating system connects the file directly to the streams.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top