0

In this question, i want to store n names in a string array but it is only storing n-1 names. It is skipping the first iteration. It works perfectly when scanf is used. Why is it happening? I am a newbie in C . Please help me out!

#include<stdio.h>
int main()
{
    int n=0;
    puts("Enter the no. of names to be saved");
    scanf("%d",&n);
    char nm[n][20];
    printf("Enter %d names\n",n);
    for(int i=0;i<n;i++)
    {
        printf("%d.",(i+1));
        gets(nm[i]);
    }
return 0;
}
  • 2
    Obligatory: [Never use `gets()`!!](https://stackoverflow.com/q/1694036/10077) – Fred Larson Dec 16 '20 at 14:50
  • 1
    Who told you to use `gets`? Don't trust that person/book to teach you C. – Lundin Dec 16 '20 at 14:53
  • 1
    And use `fgets` instead (because `gets` is no longer a part of C and shouldn't be used in any case). Even with `fgets`, you could hit the same problem. See the dups. – P.P Dec 16 '20 at 14:53

0 Answers0