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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 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! :)