I have a simple program to input the an array of 5 strings and output them. But the output is some what weird. The following is my code.
#include <stdio.h>
int main()
{
char a[10][5];
int i;
for(i=0; i<5; ++i)
{
printf("\nEnter the name of %d st student:", i+1);
fflush(stdout);
gets(a[i]);
}
for(i=0; i<5; ++i)
{
printf("\n%s", a[i]);
fflush(stdout);
}
return 0;
}
I gave the inputs as tom, john, peter, david and alan and I get the following output.
tom
john
peterdavidalan
davidalan
alan
What could be the problem?
char a[10][5]has space for 10 strings each up to 4 characters long. Trychar a[5][10] = {0};for 5 strings each up to 9 characters long, initialized to zeros.gets: It's such a bad flaw, it was deprecated before C11, and banned then.scanf("%s", buf);remains