It is not equal:
fgets (answer, 256, stdin);
if (strncmp(answer, "sta", 4) == 0)
printf("omg, it's equal");
This code is:
fgets (answer, 4, stdin);
if (strncmp(answer, "sta", 4) == 0)
printf("omg, it's equal");
Why? It is because in the first, answer doesn't have \0 at the 4th place I guess (if I change it to 3 instead of 4 it works). But what does fgets do? String answer in the first is str \whitespace*253\0"
? And in the second it is str\0
? Thank you.
fgets
(unlikegets
) includes the trailing\n
corresponding to the return pressed at the end of the line. If you put3
as the limit it truncates the string, discarding the\n
.From the manpage: