When working on the following code:
#define MAX_NAME_LENGHT 256
int main(void)
{
char name[MAX_NAME_LENGHT];
printf("Enter your name: \n");
scanf("%s", name);
if(strncmp(name, "John Smith", 10) == 0)
{
printf("Hello, John Smith!\n");
}
else
{
printf("Intruder!!!\n");
}
return 0;
}
Many errors occur and despite inputing John Smith
the output prints Intruder!!!
. However, when I replace
scanf("%s", name);
with
fgets(name, sizeof(name), stdin);
the output prints Hello, John Smith!
Why is this?