0

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.

9
  • 2
    strlen, but you have UB because char chr[] = ""; only reserves 1 byte. Try something like char chr[256] = ""; instead. Commented Oct 28, 2021 at 15:27
  • 2
    @Jack Re: "returns an int telling you how many chars have been converted" - Not quite. It will return 1 if it read a string, no matter how many chars it used up. Commented Oct 28, 2021 at 15:34
  • 1
    @yano Or safer Commented Oct 28, 2021 at 15:35
  • 1
    @yano Yeah, I updated it. I blame my tired eyes :-) Commented Oct 28, 2021 at 15:37
  • 1
    @yano I changed chr[256] and I've included <string.h>. And now it's working, thank you Commented Oct 28, 2021 at 15:43

2 Answers 2

2

to check the length of input char array (%s) using scanf()

  • Do not use the raw "%s", use a width limit: 1 less than buffer size.

  • Use an adequate sized buffer. char chr[] = ""; is only 1 char.

  • Use strlen() to determine string length when the input does not read null characters.

      char chr[100];
      if (scanf("%99s", chr) == 1) {
        printf("Length: %zu\n", strlen(chr));
      }
    
  • Pedantic: Use "%n" to store the offset of the scan if code might read null characters (this is rarely or nefariously encountered).

      char chr[100];
      int n1, n2;
      if (scanf(" %n%99s%n", &n1, chr, &n2) == 1) {
        printf("Length: %d\n", n2 - n1);
      }
    
Sign up to request clarification or add additional context in comments.

4 Comments

That %n has flown under my radar. That's so nice and actually answers the question how to get the length directly from scanf.
@TedLyngmo Note important to account for potential leading whitespace to get the right length as "%n" counts from the beginning of the scan, yet "%s" does not save leading (or any) white-space.
Got it! Niftly little option I must say! " %n%99[^\n]%n" could be an option to get the whitespaces right from the start (and include them in the string, if that's wanted).
@TedLyngmo To include white-space (except a '\n'), could use "%99[^\n]%n". Beware of special handling needed in all these formats when chr[] is only "\n".
1

sizeof is a compile time unary operator which can be used to compute the size of its operand.if you want to calculate the length of the string the you have to use strlen().like this

#include <stdio.h>
#include <string.h>
  
int main()
{
    char Str[1000];
  
    printf("Enter the String: ");
    if(scanf("%999s", Str) == 1) // limit the number of chars to  sizeof Str - 1
    {                            // and == 1 to check that scanning 1 item worked
        printf("Length of Str is %zu", strlen(Str));
    }
  
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.