There is no problem with scanf in your code. The problem is in code
char fullName[2001];
char birthday[2001];
malloc(strlen(firstName) + 1);
strcpy(firstName, fullName);
malloc(strlen(lastName) + 1);
strcpy(lastName, fullName);
printf("%s", fullName);
this should be something like
char fullName[2001];
char birthday[2001];
strcpy(fullName,firstName);
strcat(fullName,lastName);
printf("%s", fullName);
Example of working program: http://ideone.com/dvpHYL
I believe that segfault was caused by reversed arguments of strcopy
. There probably was some arbitrary data in fullName
(as it was not initialized before), and while trying to copy it to firstName
(which has smaller size) data was written beyond the bounds. malloc
s was not necessary also, but they should not cause segfaults, only memory leaks.