0

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?

dbush
  • 205,898
  • 23
  • 218
  • 273
Leon
  • 1
  • 1
  • 3
    `gets()` reads a whole line. The line is longer than 7 characters, so there isn't enough room in `str2`, and you get a buffer overflow. – Barmar May 03 '23 at 17:24
  • `gets` does not "scan" the input, it just reads it. There is no parsing of any kind, other than checking for the newline to terminate the read. – William Pursell May 03 '23 at 17:27
  • What did you expect would happen when you typed 19 characters and asked `gets` to read them (plus a newline, plus a terminating `\0`) into an array of size 8? – Steve Summit May 03 '23 at 17:27
  • @SteveSummit I didn't know what to expect, I was trying different ways to approach gets so I can understand it. – Leon May 03 '23 at 17:35
  • @Büşra Super-important thing to know: In C, memory allocation is usually *your* responsibility. So — learn this now — what you should have expected would have happened was "array overflow with arbitrarily bad consequences". You cannot store 21 characters into an array of size 8. What happens (if you try) is not that you get a nice error message saying "array overflow: trying to store 21 characters into an array of size 8". What happens is not that the array automatically grows to be big enough. No, what happens is something strange, unpredictable, dangerous, and not useful. – Steve Summit May 03 '23 at 17:39
  • Since memory allocation is mostly your responsibility, you usually have to explicitly keep track of how big things are. If you call the `fgets` function, you tell it how big your array is, so that `fgets` can be sure not to store more into it than there's room for. But `gets` doesn't have this ability. When you call `gets`, it has no way of knowing how big the array is, so it has no way of avoiding overflowing that array. This inability makes the `gets` function practically useless, which is why it was actually removed from the C language many years ago. – Steve Summit May 03 '23 at 17:40
  • Separately from the differences in ways of working, read [Why `gets()` is so dangerous it should never, ever be used!](https://stackoverflow.com/q/1694036/15168). Your teacher needs to drop `gets()` from their curriculum. You need to assume it is implemented as `char *gets(char *s) { abort(); }` — that is, your program will terminate unilaterally with extreme prejudice if you call it. Your program exhibits "undefined behaviour". – Jonathan Leffler May 03 '23 at 23:40

0 Answers0