this post was submitted on 31 May 2026
6 points (100.0% liked)

C Programming Language

1309 readers
2 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

ยฉ Dennis Ritchie

๐ŸŒ https://en.cppreference.com/w/c

founded 3 years ago
MODERATORS
 

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);  

top 7 comments
sorted by: hot top controversial new old
[โ€“] lemmysmash@piefed.social 5 points 1 week ago* (last edited 1 week ago) (1 children)

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.

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

[โ€“] hkwln@lemmy.ml 3 points 1 week ago (1 children)

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 ๐Ÿคฃ

[โ€“] kiri@ani.social 1 points 1 week ago* (last edited 1 week ago) (1 children)

read #1.5 and #7: K&R

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

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

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.