I am trying to find the length of the input string using fgets
and strlen
but I have came across some issues where the null terminating character is unable to be detected.
By empty input string I mean just pressing enter without entering anything.
I have used strlen
on the input of empty string and outputs 1
where is should be 0
.
int func(char *input) {
if (input[0] == '\0')
printf("null detected\n");
else
printf("null not detected\n");
printf("len: %lu", strlen(input));
}
int main() {
char input[256];
fgets(input, sizeof(input), stdin);
func(input);
return 0;
}
output: null not detected
correct output: null detected