Echoing Characters with getchar() in a Loop

  • Thread starter keltix
  • Start date
  • Tags
    Loop
In summary, The conversation discusses the use of getchar() to capture characters and print them to stdout in a loop. It explains why the characters are first captured and then printed all at once, and how to echo each character one at a time using getchar() loop. The use of _getch() instead of getchar() is suggested for successful output, and the need for flushing stdout is mentioned. It is also noted that on some systems, individual keystrokes are not captured until enter is pressed, and the use of system-specific APIs is recommended for trapping individual keystrokes.
  • #1
keltix
42
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
  • #3
Try _getch() instead.
 
  • #4
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.
 
  • #5
that just reads without echoing (i.e. w/o printing strokes on screen)
 
  • #6
Coin, the fflush didn't work.

good guess though
 
  • #7
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.
 
  • #8
system-specific API?

idk what that is, but yea it only works after pressing enter
 
  • #9
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.
 

FAQ: Echoing Characters with getchar() in a Loop

1. What is the purpose of using getchar() in a loop when echoing characters?

The purpose of using getchar() in a loop when echoing characters is to continuously read input from the user and print it back to the screen until the user presses the "Enter" key. This allows for interactive programs where the user can enter multiple characters in a single line.

2. How does getchar() work to echo characters in a loop?

Getchar() works by reading a single character from the user's input and returning its ASCII code. This character can then be printed to the screen using the printf() function. In a loop, getchar() can be used to continuously read and echo characters until a certain condition is met.

3. Can getchar() be used to echo multiple characters at once?

No, getchar() can only read and echo one character at a time. This is because it reads from the standard input stream, which is typically line buffered, meaning it will only read one line at a time. To echo multiple characters, you would need to use a different function like fgets() or scanf().

4. How can getchar() be used to validate user input?

Getchar() can be used to validate user input by checking the ASCII code of the character returned. For example, if the user is only allowed to enter numbers, you can use the isdigit() function to check if the returned ASCII code is a digit. If it is not, you can prompt the user to enter a valid input.

5. Is it necessary to use a loop when echoing characters with getchar()?

Yes, using a loop is necessary when echoing characters with getchar(). This is because getchar() will only read and return one character at a time. If you want to continuously read and echo characters, you need to use a loop to keep calling getchar() until the user enters a terminating character, such as "Enter".

Similar threads

Replies
19
Views
4K
Replies
6
Views
2K
Replies
3
Views
1K
Replies
16
Views
2K
Replies
4
Views
3K
Replies
12
Views
2K
Back
Top