5

I was experimenting with while loops, testing what happens if I condition the loop with == 0/1/EOF versus != 0/1/EOF. Obviously, this shows, more than anything else, my lack of understanding of the logic behind these operations, but it also taught me that getchar() will return the ASCII value of chars and ints if I use the format specifier %d. Also, that scanf() isn't as "forgiving", causing infinite loops, among other things, if I under certain conditions enter a char.

Is there anything else that I could learn/take away here, or was this a waste of time? ๐Ÿ˜…

The various results are commented next to the respective loops.

#include <stdio.h>  

int main() {  

	int number = 0;  

	printf("Enter a number: ");  

/*  
	while((scanf("%d", &number)) != 1) { //Success with "== 1" or "!= 0/EOF", terminates after int input with "== 0/EOF" or "!= 1", terminates after char input with "== 1" or"!= 0", infinite loop after char input with "== 0" or "!= 1/EOF".  
		printf("You have entered number %d\n", number);  
		printf("Enter a new number: ");  
	}  
*/  

/*  
	while ((number = getchar()) == EOF) { //Success with "!= 0/1/EOF", terminates after input with "== 0/1/EOF".  
		getchar();  
		printf("You have entered number %d\n", number);  
		printf("Enter a new number: ");  
	}  
*/  

	return 0;  
}  

you are viewing a single comment's thread
view the rest of the comments
[-] akunohana@piefed.blahaj.zone 2 points 3 weeks ago* (last edited 3 weeks ago)

Ah, forgot about those. Thanks! On Linux, I read man pages all the time. Need adopt that same rutine.

Regarding getchar() returning the ASCII code of the input, I commented this is the OP:

getchar() will return the ASCII value of chars and ints if I use the format specifier %d.

this post was submitted on 06 May 2026
5 points (100.0% liked)

C Programming Language

1303 readers
1 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 2 years ago
MODERATORS