r/C_Programming • u/Professional-Cod1776 • Jun 28 '26
Trying to understand why thing 'hangs'
Complete beginner; I encountered while( getchar() != '\n'); so trying to understand this stuff better but this program hangs instead of exiting and I cant figure out why.
input: abcd\n\n\n
#include <stdio.h>
int main() {
char a;
scanf( "%c", &a);
while(getchar() != '\n');
while(getchar() == '\n') {
while(getchar() == '\n') return 0;
}
printf("executed\n");
return 0;
}
15
Upvotes
19
u/Atijohn Jun 28 '26
are you literally typing
\nin the terminal, or are you pressing enter?6
u/kappakingXD Jun 28 '26
Are you sure the process hangs? By the look of it, it probably exists through return 0 before printing anything.
2
u/ReallyEvilRob Jun 28 '26
If you're using scanf() for getting input, then there's no need to use getchar(). Just add a newline character to the format string after %c so scanf() will expect the newline.
If you are going to get input using getchar(), then get rid of the scanf() call. Also, you only need to call getchar() once in your loop. The loop should go something like this:
Something to understand is that getchar() actually returns an int, not a char. The reason for that is EOF is signaled by -1. If you assign the return value of getchar() directly to a char, some valid characters will get sign-extended to -1 and be interpreted as EOF. The character 'ÿ' has ascii value 255 which would make the loop terminate early.
2
u/bare_metal_C Jun 29 '26
it isn't hanging, it is waiting for your input and that line while(getchar() != '\n'); flushes the input buffer
24
u/wizarddos Jun 28 '26
Why are you using 2 while loops? 1 should be enough for that
7
u/mjmvideos Jun 28 '26
Step through it.
1
u/Total-Box-5169 Jun 28 '26
The function getchar() returns a negative upon failure, like end of file. Probably your code hits a never-ending loop when checking against the endline character, because your input doesn't contain a proper endline character but instead a backslash followed by n.
3
u/rb-j Jun 28 '26
getchar()andputchar()are functions that interact real-time with the console if that's the standard input and output.putchar()shouldn't hang normally with the console, butgetchar()is waiting for you to type a character.1
u/ForgedIronMadeIt Jun 29 '26
Attach a debugger and single step through the code and examine what is going on. It's a valuable skill to develop.
1
u/duane11583 Jun 29 '26
my experience is that getcgar() should omly be used when the app is reading from a file (ie io redirection), ie “cat file | ./myapp”
if you are actively interacting with the app (what you describe typing keys) you want a to do it a different way.
1
u/GreenAppleCZ Jun 29 '26
'\n' is a special ascii character meaning new line.
You could see text files as huge arrays of bytes, each representing a character. So, the file needs a way to know where to break the line. And the way it's done is through the special character. So you don't type "\n", you just press enter to put a new line.
You should also learn about EOF (end of file), which is a special constant that is returned by getchar if the entire file is read. If you're reading the entire file, it's safest to have an EOF check that terminates the reading somewhere.
1
u/ScarcityTerrible5330 Jun 28 '26
I think getchar() gets one character at a time. so literally you think that the program with take a newline character as “\n” but in actually if you’re typing it its just “\” and “n”.
0
u/FinalNandBit Jun 28 '26
What is getchar()?
1
u/[deleted] Jun 30 '26
[removed] — view removed comment