0

The fragment of code below doesn't work properly.

#include <stdio.h>
#include <string.h>
int main(){
    int num;
    char str[40];
    scanf("%d",&num);
    gets(str);

    return 0;
}

It seems that the scanf of an integer is impacting on the reading of the string with gets. I think that we could deal with this by using the fflush (stdin), but this is not in the ANSI C standard.

I would like to know if there is some standard way to deal with this in ANSI C.

Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • 3
    Don't use `gets`. At all. Use `fgets` instead. And your program has no output, so it can't "not work properly". – Eugene Sh. Apr 16 '19 at 17:32
  • Flushing an *input* stream has no defined effect. Also, never use `gets()`. It is unsafe, and has been removed from the language (though it may still be avilable to you as an extension). – John Bollinger Apr 16 '19 at 17:32
  • 1
    probably your problem concerns the newline you enter after the num ... – bruno Apr 16 '19 at 17:33

0 Answers0