-1

The puts function doesn't print the 3 strings as i input them. They contain garbage.

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

int main ()
{
    char para[20][3];
    int x;

    for(x=0; x<3; x++)
    {
      gets(para[x]);
      fflush(stdin);        
    }

    for(x=0; x<3; x++)
      puts(para[x]);

    return 0;
}

1 Answers1

2
fflush(stdin);   

is undefined behavior. It should be one output or update stream which is passed to fflush. This, what you have done might result in something weird to normal behavior.

gets is deprecated and not meant to be used. Use fgets instead. You should be able to enter 2 characters and a \n with the array size shown. Maybe you meant para[3][20]. gets has no safety over buffer overflow. That is there is fgets in the form of specifying the number of characters you want to read.

user2736738
  • 30,591
  • 5
  • 42
  • 56