char Name[21]; int main() { printf("What's your name?:\t"); scanf("%[^\n]", &Name); printf("Well, then hello, world from %s", Name); system("pause"); return 0; } |
scanf("%[^\n]", &Name);
has two errors:&Name
is not a pointer to char, it is a pointer to an array of 21 char. The correct syntax is &Name[0]
or just Name
.%20[^\n]
in this case (20 letters and 1 null terminator), instead of the unlimited %[^\n]
. Otherwise someone could type more than 20 characters and cause buffer overrun, which leads to anywhere from crashing to having all your passwords stolen.