Below is a program that asks the user
to type in a name, and then prints out the name one letter
at a time, eventually producing the whole name.
I have two versions of the program. The one below
does a strange thing: if I type in something like
jkljljkliunionnklsaa
, it will produce only jkljljkl
.
But if I restrict char name[40]
, and then type in the same name, the program shows the whole name.
What have I done wrong in the program that doesn't allow it to produce the whole name?
#include <stdio.h>
#include <string.h>
int main (void)
{
int i, s;
char name[s];
printf ("Type a name: ");
scanf ("%s", name);
for ( i = 0; i < strlen(name); i++)
{
printf ("%c", name[i]); // print the i-th character of name
}
printf("\n");
}