I need to check the length of an input using the function scanf().
I'm using an array of char (%s) to store the input, but I wasn't able to check the length of this input.
this below is the code:
#include <stdio.h>
char chr[] = "";
int n;
void main()
{
printf("\n");
printf("Enter a character: ");
scanf("%s",chr);
printf("You entered %s.", chr);
printf("\n");
n = sizeof(chr);
printf("length n = %d \n", n);
printf("\n");
}
it's giving me back that "length n = 1" for the output in each case I've tried.
How can I check the length of the input in this case? Thank You.
strlen, but you have UB becausechar chr[] = "";only reserves 1 byte. Try something likechar chr[256] = "";instead.inttelling you how many chars have been converted" - Not quite. It will return1if it read a string, no matter how many chars it used up.