C getchar vs scanf - c

I am confused by a piece of code found in a function I am studying:
char GetCommand( void )
{
char command;
do {
printf( "Enter command (q=quit, n=new, l=list): " );
scanf( "%c", &command );
Flush();
}
while ( (command != 'q') && (command != 'n')
&& (command != 'l') );
printf( "\n----------\n" );
return( command );
}
void Flush( void ) {
while ( getchar() != '\n' )
;
}
What I don't quite understand here is the usage of the Flush() function. I mean, the book I am reading explains it by saying that it prevents the user from inputting more than a single character and then having that character read when they are prompted for input the 2nd time.
What I don't understand is how Flush() is preventing this from happening. It doesn't DO anything. All it is is a while command. (While this is true......what?????) Doesn't make sense.

getchar() has the side effect of removing the next character from the input buffer. The loop in Flush reads and discards characters until - and including - the newline \n ending the line.
Since the scanf is told to read one and only one character (%c) this has the effect of ignoring everything else on that input line.
It would probably be more clear if the scanf was replace with
command = getchar();
but it's actually a generally bad example as it does not handle End Of File well.
In general scanf is best forgotten; fgets and sscanf work much better as one is responsible for getting the input and the other for parsing it. scanf (and fscanf) try to do too many jobs at once.

getchar reads one character from standard input. If you put it in a while loop, it will continue to read one character at a time until the condition is false.
What the Flush function is doing is reading until it encounters a newline (\n). This is the character produced when the user hits the enter key.
So, the code you gave will read one character (I'm unclear on why it uses scanf for this instead of simply getchar, which would be faster), and then discards the rest of the input until the user hits enter.
If you were to feed this program foobar, it would take the f and discard the oobar in the Flush function. Without calling flush, the f could go to one scanf, and the second scanf would get the first o.

When you enter your character and press Enter, a newline character is generated by you pressing the Enter key and it remains in the buffer. This is problematic because it will wait around until the next time you require user input and it will be used for that input. Flush is used to flush the newline character from the input buffer so you don't have that problem. Flush actually uses the newline in the input buffer when it reads it and it is discarded, so it is no longer in the buffer.

Related

I am not able to execute a code in C | Some sort of error that doesn't show up [duplicate]

The following code produces a very strange result when I run it.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for ( ; ; )
{
char test;
printf("Please enter 'w' ");
scanf("%c", &test);
printf("%c\n", test);
if (test == 'w')
{
printf("Working\n");
}
else
{
printf("ERROR\n");
return 0;
}
}
}
What I want to happen is for the whenever I input 'w' it continues the loop so I can input 'w' again. What it does though is go to the else statement even though I input 'w'. It just seems to skip the scanf() line. I have asked everyone I know who knows C but they do not know how to solve it.
Somebody please help me out here!
This is because you type w followed by ENTER. So there are 2 characters in the input, 'w', followed by a newline (\n). The latter causes the else branch to be taken on the second iteration.
Note that standard input is line buffered when connected to a terminal. If you need to deal with characters immediately, there are ways to do that. See the comp.lang.c FAQ for details ("How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed?").
Note that for robust programming it is a must to check the return value of scanf. It returns the number of successfully converted items. As shown, your code does not handle the case of end-of-file properly, i.e. when the user types Ctrl-D (assuming Unix terminal). Then scanf returns EOF and no conversion was performed, but you use test as if it contained a meaningful value.
as Jens said. you have to ignore the newline '\n'
Adding a space at the beginning of the format specifier " %c" will ignore the newline '\n'
scanf(" %c", &test);
Using " %c" will also ignore other white spaces like \t space \b \v \r
As Jens says, you must consume '\n', use getchar() after scanf()
You need to do something like
scanf("%c", &test);
while(getchar()!='\n');
scanf takes input upto space or \n (whichever comes first) and leaves the \n in the buffer

Explanation about detecting end of line in C

I'm reading/practicing with this book about C language: "C Programming - A Modern Approach 2" and I stumbled upon this piece of code whose explanation is odd to me:
Since scanf doesn't normally skip white spaces, it's easy to detect the end of an input line: check to see if the character just
read is the new-line character. For example, the following loop
will read and ignore all remaining characters in the current input
line:
do {
scanf("%c", &ch);
} while (ch != '\n');
When scanf is called the next time, it will read the first character on the next input line.
I understand the functioning of the code (it exits the loop when it detects enter or '\n') but not the lesson(?).
What is the purpose of this, since to store the input from the user into &ch you HAVE to press the enter key (quitting the loop)?
Also what does "the following loop will read and ignore all remaining characters in the current input line" mean actually?
...What is the purpose of this...
For context in understanding what this code snippet can be used for, take a look at this link discussing stdio buffering.
In simplest logical terms, this technique consumes content placed in stdin until the newline character is seen. (From a user hitting the <enter> key.) This is evidently the lesson.
This effectively clears the stdin buffer of content, also described in this more robust example.
Also what does
"the following loop will read and ignore all remaining characters in
the current input line"
mean actually?
do {
scanf("%c", &ch);
} while (ch != '\n');
It means that if the new char just read by scanf is not equal to the newline character, defined as \n, it will continue to overwrite c with the next read. Once ch does equal \n, the loop will exit.

while (getchar () != '\n' );

I have the following for loop, I am prompting the user to enter a 4 digit pin and hit enter. Can someone explain to me what the while loop is really doing because I don't fully understand it.
//user input for pin
for(i = 0; i < PIN_LENGTH; i++)
{
printf("Enter digit %d of your PIN: ", i);
user_pin[i] = getchar();
while (getchar() != '\n'); //what is this line doing??
}
As mentioned by others, this loop discards unwanted characters from stdin so that the next input function has a clean stream, and in particular it discards the \n that follows the last character entered by the user. But, getchar() returns EOF in the event of a read error, so the loop should test for EOF also, like this:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF)
continue; // discard unwanted characters
Also note that, if stdin has been redirected, it is possible to reach EOF without encountering a \n. And, as #chqrlie pointed out in the comments, the user can signal an EOF from the terminal by entering Ctrl-D in Linux, or Ctrl-Z in Windows. Hence the importance of testing for EOF explicitly.
When you give input to the program, then you end it with the Enter key. This key is sent to your program as a newline.
What the loop does is to read and discard characters until it has read the newline. It flushes the input buffer.
If you don't do this, then the next main getchar call would return the newline instead of the digit you expect.
The next line is discarding the possible extra chars that the user may have inputted, and also the linefeed char that the user had to input.
So other scanf/getchar methods further in the code are not polluted by this spurious input.
The while loop is used to check when the user will press enter.Enter is considered as '\n'.
That line is a while, so at run-time it will repeat it until getchar() == '\n'. '\n' is a character that in ASCII means "enter" key. In this way you can test that the user will not press "enter" without had inserted a number.
**The function of this while loop is to clear the users' illegal input .When the input is '\n' ,the loop ends.

My code doesn't work when using scanf("%c") [duplicate]

The following code produces a very strange result when I run it.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for ( ; ; )
{
char test;
printf("Please enter 'w' ");
scanf("%c", &test);
printf("%c\n", test);
if (test == 'w')
{
printf("Working\n");
}
else
{
printf("ERROR\n");
return 0;
}
}
}
What I want to happen is for the whenever I input 'w' it continues the loop so I can input 'w' again. What it does though is go to the else statement even though I input 'w'. It just seems to skip the scanf() line. I have asked everyone I know who knows C but they do not know how to solve it.
Somebody please help me out here!
This is because you type w followed by ENTER. So there are 2 characters in the input, 'w', followed by a newline (\n). The latter causes the else branch to be taken on the second iteration.
Note that standard input is line buffered when connected to a terminal. If you need to deal with characters immediately, there are ways to do that. See the comp.lang.c FAQ for details ("How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed?").
Note that for robust programming it is a must to check the return value of scanf. It returns the number of successfully converted items. As shown, your code does not handle the case of end-of-file properly, i.e. when the user types Ctrl-D (assuming Unix terminal). Then scanf returns EOF and no conversion was performed, but you use test as if it contained a meaningful value.
as Jens said. you have to ignore the newline '\n'
Adding a space at the beginning of the format specifier " %c" will ignore the newline '\n'
scanf(" %c", &test);
Using " %c" will also ignore other white spaces like \t space \b \v \r
As Jens says, you must consume '\n', use getchar() after scanf()
You need to do something like
scanf("%c", &test);
while(getchar()!='\n');
scanf takes input upto space or \n (whichever comes first) and leaves the \n in the buffer

scanf in a while loop reads on first iteration only

NOTE: Please notice this is not a duplicate of Why is scanf() causing infinite loop in this code? , I've already seen that question but the issue there is that he checks for ==0 instead of !=EOF. Also, his problem is different, the "infinite loop" there still waits for user input, it just does not exit.
I have the following while loop:
while ((read = scanf(" (%d,%d)\n", &src, &dst)) != EOF) {
if(read != 2 ||
src >= N || src < 0 ||
dst >= N || dst < 0) {
printf("invalid input, should be (N,N)");
} else
matrix[src][dst] = 1;
}
The intention of which is to read input in the format (int,int), to stop reading when EOF is read, and to try again if an invalid input is received.
The probelm is, that scanf works only for the first iteration, after that there is an infinite loop. The program does not wait for user input, it just keeps assuming that the last input is the same.
read, src, and dst are of type int.
I have looked at similar questions, but they seem to fail for checking if scanf returns 0 instead of checking for EOF, and the answers tells them to switch to EOF.
You need to use
int c;
while((c=getchar()) != '\n' && c != EOF);
at the end of the while loop in order to clear/flush the standard input stream(stdin). Why? The answer can be seen below:
The scanf with the format string(" (%d,%d)\n") you have requires the user to type
An opening bracket(()
A number(For the first %d)
A comma(,)
A number(For the last %d)
The space(First character of the format string of your scanf) and the newline character(\n which is the last character of the format string of your scanf) are considered to be whitespace characters. Lets see what the C11 standard has to say about whitespace characters in the format string of fscanf(Yes. I said fscanf because it is equivalent to scanf when the first argument is stdin):
7.21.6.2 The fscanf function
[...]
A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails
So, all whitespace characters skips/discards all whitespace characters, if any, until the first non-whitespace character as seen in the quote above. This means that the space at the start of the format string of your scanf cleans all leading whitespace until the first non-whitespace character and the \n character does the same.
When you enter the right data as per the format string in the scanf, the execution of the scanf does not end. This is because the \n hadn't found a non-whitespace character in the stdin and will stop scanning only when it finds one. So, you have to remove it.
The next problem lies when the user types something else which is not as per the format string of the scanf. When this happens, scanf fails and returns. The rest of the data which caused the scanf to fail prevails in the stdin. This character is seen by the scanf when it is called the next time. This can also make the scanf fail. This causes an infinite loop.
To fix it, you have to clean/clear/flush the stdin in each iteration of the while loop using the method shown above.
scanf prompts the user for some input. Assuming the user does what's expected of them, they will type some digits, and they will hit the enter key.
The digits will be stored in the input buffer, but so will a newline character, which was added by the fact that they hit the enter key.
scanf will parse the digits to produce an integer, which it stores in the src variable. It stops at the newline character, which remains in the input buffer.
Later, second scanf which looks for a newline character in the input buffer. It finds one immediately, so it doesn't need to prompt the user for any more input.

Resources