[SOLVED] C: behaviour of getchar() after a print statement that ends in a whitespace???
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C: behaviour of getchar() after a print statement that ends in a whitespace???
Maybe a beginner's question but then I have been learning C for only two days...
How do I make getchar() skip a leading space? The situation I am looking is this: I print a prompt that ends in a space
printf("Input: ");
and then when I use getchar() to read the text that was typed after the prompt, it appears to process the trailing space as a leading space that belongs to the input.
Could anyone explain why? And how can the space be skipped, preferably without inserting code that explicitly checks whether I'm reading the first character or not. I have already found out that I can skip such a space using scanf
scanf (" %c", &ch);
where the leading space in the format string eliminates leading spaces from the input string. That is the sort of thing I have in mind.
Maybe a beginner's question but then I have been learning C for only two days...
...when I use getchar() to read the text that was typed after the prompt, it appears to process the trailing space as a leading space that belongs to the input. ...
'getchar' is buffered, i.e. new characters are seen only after you enter newline ('\n').
So, if, for example, you want to enter 'a', in fact you need to enter 'a', '\n'. So after the first call to 'getchar' you'll get 'a' (that's what you expect), but on the second call (suppose, you wanted to enter 'b') instead of 'b' you'll first get the '\n' you've entered after 'a', and only on the third call you'll get 'b', but again you'll have yet another '\n' in the buffer.
That won't help. The problem is not fread() versus getchar(); you need to use termios to make your data unbuffered. Then you can use either getchar() or fread().
To observe how fread() acts buffered under ordinary circumstances, run this shell script:
That won't help. The problem is not fread() versus getchar(); you need to use termios to make your data unbuffered. Then you can use either getchar() or fread().
...
Thanks for your suggestions but I'm afraid I don't see how they answer my question. Like I said, the problem is that part of my output is being considered as input:
Code:
printf("Input: "); //print "Input: " prompt
char ch = getchar(); //read input entered at the prompt
If I run the code and enter, say, an 'a', I would expect ch to hold the value 'a'. But it doesn't, it contains a space. The only reason I can think of is that somehow the trailing space of the prompt (there is a space after the colon) is being treated as input. But why?
Terminal uses UTF.8. When I run your piece of code, I also get 41 hex so the problem must lie elsewhere. I think I'll need to take a better look at the code that was producing the problem. It seems quite likely that some method call before the printf statement leaves a new line in the buffer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.