I need to get input for n
(user-inputted) strings. For that I start with defining a two-dimensional array char str[ ][ ].
I used for
loop getting input from user and have tried gets()
, fgets()
both.
In code example although I used gets().
But it is always taking input for n-1 strings, i.e, 1 less than user want to enter.
Upon further checking I found that program is not taking input for 0th string, i.e., initial string.
My code:
#include <stdio.h>
int main(void){
int i, n;
printf("how many string you want to enter: ");
scanf("%d", &n);
char str[n][60];
printf("start entering strings:\n ");
for(i=0;i<n;i++){
gets(str[i]); //have used here fgets() also
}
puts(str[0]); //no output for Oth string
return 0;
}
Output:
how many string you want to enter:
User input - 3
how many string you want to enter: 3
start entering strings:
Final Output:
how many string you want to enter: 3
start entering strings:
abc
bcd
Here program terminates after taking input for only 2 strings and not giving any ouput for puts(str[0]);
Although taking input with scanf()
as scanf("%s", str[i]);
worked perfectly fine.
I want to know why using gets()
, fgets()
didn't worked.