gets and scanf way of reading input

43 views Asked by At

I have a simple code that I wrote to differentiate the way gets and scanf scan code, I've read about their differences and I understand them, my problem specifically is with the output that gets show.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    
    char str1[8];
    char str2[8];

    gets(str2);

    scanf("%s", str1);

printf("%s\n%s", str1, str2);

    return 0;
}

if I enter these strings:

b b b b b b b b b b

d d d d d

the output is as follows:

d

b b b b b b b b d

my confusion is to why is there a "d" at the end of the "b" string? why is gets scanning characters after the new line?

0

There are 0 answers