RetroFed Forums
Your home away from modern life.
Is this a limitation of scanf() or bad control flow?
Post Reply
AuthorMessage
printf("%s", name); piefed.blahaj.zone akunohana@piefed.blahaj.zone
Joined: 2026-03-30
Quote

Good afternoon! How are you all holding up today? I "accidentally" ate piece of cake at the cafe I'm currectly sitting in, even though I had promised myself not to eat sugar until next week. Fail. 😅

Don't mind the English language, since this is an excerpt from a larger piece of code.

If I enter anything other than 'Y' and 'N' using a single character, the code works as I want it to, but, naturally, if I enter anything other than 'Y' and 'N' multiple times, the if is executed the same amount of times as characters entered, which is ugly. Trying to limit this with %1c also doesn't work, since I suppose that only works with strings? Is this a limitation of scanf or rather how the logic is implemented?

Feel free to NOT provide the correct answer right away, but instead, give me the topic or the function to read up on. 😊

	char letter = '\0';  
	while(1) {  
		printf("nter the letter 'Y' or 'N': ");  
		scanf(" %c", &letter);  
		if (letter != 'Y' && letter != 'N') { //"broken" because multicharacter input executes this condition that many times.  
			printf("You have to enter 'Y' or 'N'! You have entered %c!\nE", letter);  
		}  
		else { break; }  
	}  
	printf("Good job! You have entered: %c.\n", letter);  

8   Original
kiri ani.social kiri@ani.social
Joined: 2024-06-08
#2 · (edited) Quote Reply

read #1.5 and #7: K&R

or just find examples of getchar() or fgets()

3
lemmysmash piefed.social lemmysmash@piefed.social
Joined: 2026-02-26
#3 · (edited) Quote Reply

I'm too lazy to confirm, but I think scanf (as well as many other I/O functions) works on top of the buffered input. I.e. when you scanned only one character on the first run of scanf but there are more left in the buffer, then subsequent calls will read from that buffer, e.g.

printf("your prompt>")
/* you type 123 */
/* buffer contains: "123" */
scanf(...)
/* you scanned 1 char ("1") */
/* buffer contains: "23" */
printf("new prompt>");
/* the prompt has been printed AFTER your previous input but the buffer hasn't been reset! */
/* buffer contains: "23" */
/* you type 456 */
/* buffer contains: "23456" */
scanf(...)
/* scanf returns "2" */
/* buffer contains: "3456" */

In general you have two options:

  1. drain the buffer after / before each loop cycle
  2. use unbuffered I/O

For both cases, do your research on how to do it :) That K&R from above should be a great start indeed.

6
printf("%s", name); piefed.blahaj.zone akunohana@piefed.blahaj.zone
Joined: 2026-03-30
#4 · Quote Reply

read #1.5 and #7: K&R or just find examples of getchar() or fgets()

Thanks! I'll look 'em up! :)

2
printf("%s", name); piefed.blahaj.zone akunohana@piefed.blahaj.zone
Joined: 2026-03-30
#5 · Quote Reply

I'm too lazy to confirm, but I think scanf (as well as many other I/O functions) works on top of the buffered input. I.e. when you scanned only one character on the first run of scanf but there are more left in the buffer, then subsequent calls will read from that buffer, e.g. printf("your prompt>") /* you type 123 */ /* buffer contains: "123" */ scanf(...) /* you scanned 1 char ("1") */ /* ...

Thanks very much! I had no idea there is such a thing as "unbuffered I/O". I'll look into it. :)

3
hkwln lemmy.ml hkwln@lemmy.ml
Joined: 2025-02-16
#6 · Quote Reply

you could also use getchar() to only take the first char out of the buffer, if you didn't know already ;)

3
ExperimentalGuy programming.dev ExperimentalGuy@programming.dev
Joined: 2023-07-25
#7 · Quote Reply

Use gets on a stack buffer, but don't enable stack canaries and make the stack both writable and executable. Your users will thank you.

1
printf("%s", name); piefed.blahaj.zone akunohana@piefed.blahaj.zone
Joined: 2026-03-30
#8 · Quote Reply

you could also use getchar() to only take the first char out of the buffer, if you didn't know already ;)

I'll try this soon. 😊 To my defense, I don't have a bathtub in which I could arrive at these kinds of ideas 🤣

2
iusemybrain sh.itjust.works iusemybrain@sh.itjust.works
Joined: 2026-06-06
#9 · Quote Reply

I would do the loop instead the if condition, where letter != 'Y' or 'N' generally speaking, using while(1) for x86_64 programming is bad practice, specific cases like embedded system design is where it's more commonly used.

1
Post Reply
Back to top