Echoing Characters with getchar() in a Loop

  • Thread starter Thread starter keltix
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around the behavior of the getchar() function in C programming, specifically in the context of echoing characters one at a time in a loop. Participants explore the challenges of immediate character output versus buffered output, and the potential need for alternative functions or system-specific APIs.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions why getchar() captures characters and prints them all at once, seeking a method to echo characters individually.
  • Another participant references a sample output from an external link, indicating that their code does not produce the expected results.
  • A suggestion is made to use _getch() instead of getchar() for immediate character capture.
  • Concerns are raised about the availability of _getch() across different systems.
  • One participant explains that stdout buffers output for efficiency and suggests using fflush(stdout) to force the buffer to flush after each character output.
  • Another participant reports that fflush(stdout) did not resolve their issue of echoing characters.
  • A participant notes that many systems require pressing enter before any characters are received, indicating a limitation of standard input handling.
  • Further clarification is provided regarding system-specific APIs, with mentions of _getch() in the Windows API and similar functions in the curses library for *NIX systems.

Areas of Agreement / Disagreement

Participants express differing views on the behavior of getchar() and the effectiveness of fflush(stdout). There is no consensus on a single solution, as multiple approaches and system-specific considerations are discussed.

Contextual Notes

Limitations include the dependence on system-specific APIs for immediate character capture and the buffering behavior of stdout in C, which may not be universally applicable across all environments.

keltix
Messages
42
Reaction score
0
If you use getchar() to capture a character and then print it to the stdout in a loop,
why does it first capture the characters in the list (up to whatever max) and then print them all at once?

In other words, how would I echo every character one at a time using a getchar() loop?
 
Technology news on Phys.org
Try _getch() instead.
 
Is _getch() available everywhere?

What is happening to you I think is that you need to flush stdout. In C if you write to a file buffer (stdout is a kind of file buffer) it will buffer a bunch of writes and then write them all at once, for efficiency. "Flushing" forces the write-everything-buffered-at-once operation to happen earlier.

Try putting:
fflush(stdout);
After putch.
 
that just reads without echoing (i.e. w/o printing strokes on screen)
 
Coin, the fflush didn't work.

good guess though
 
On many (most? all?) systems, your program isn't given any of the characters until you press enter. You need to use some system-specific API if you want to trap individual keystrokes.
 
system-specific API?

idk what that is, but yea it only works after pressing enter
 
keltix said:
system-specific API?
Yah. Things like the _getch() function in the Windows API Jeff mentioned. I believe the curses library (*NIX systems) has a similar function.

And, of course, any API that let's you make windows and stuff should have ways to get keypresses.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K