i am new to c and have used python before. This whole buffer overflow stuff is really breaking my mind.
#include <stdio.h>
int main(){
char str1[3];
while(true){
scanf("%2s", str1);
printf("test\n");
}
}
This is a little code i've written to test the syntax and the stdio library. I was really suprised when the program outputted "test" multiple times, depending on how many characters i entered. So for example, when I entered two characters, it printed "test" two times. Can anyone please tell me why this is happening and how I can fix it?
CodePudding user response:
You need to clear your input buffer as per this answer
Otherwise, you'll read from the stdin, print it, jump back to the loop head and continue reading, if there is still something in the buffer.
CodePudding user response:
Each time through the loop, scanf("%2s", str1)
reads at most 2 non-whitespace characters from the input stream. If there are more than 2 non-whitespace characters available in the stream, the loop will continuously call scanf
(and printf
) until scanf
blocks waiting for data. If the input stream contains ffff\n
and has not yet been closed (eg, a user is entering data interactively from a tty), the first 2 calls to scanf
will immediately return and printf
will be called twice. The 3rd call to scanf will block until more data is available, or the stream is closed, or there is an error.
CodePudding user response:
You can figure out what happens by modifying your code as follows:
#include <stdio.h>
int main(){
char str1[3];
while( 1 ){
scanf("%2s", str1);
printf("test: %s\n", str1);
}
}
which simply prints the contents of the str1
alongside of the "test" string.
Here is an example output for an input string of 1234567
:
1234567
test: 12
test: 34
test: 56
test: 7
The scanf("%2s", str1);
statement reads two characters from the stdin
and assings them to the str1
. The read characters are "popped" from the input stream, i.e., they are removed. If the stdin
happens to contain more characters, the excess ones are left untouched. Therefore, for the given input, when the first scanf
is returned, the str1
containes 12\0
, and the stdin
contains 34567
.
Since these are in the infinite loop, the code repeats, scanf
gets called again, reading the first two characters from the stdin
again, only this time finds 34
.
And the process repeats, untill there are no characters left on the stdin
, then the scanf
waits for the user input, presumably as you would have expected.
Basically, scanf
keeps reading instead of waiting for user input, since the stdin
already contains something to read.
So for example, when I entered two characters, it printed "test" two times.
This on the other hand, does not make sense, as it should be printing "test" for N/2 times, rounded up, where N is the number of characters you enter.
There is not much that I can suggest for "fixing this", since it is not really clear what you are expecting. But if you want to get rid of the remaining characters in the stdin
, you can check this.