-3

When returning the length and the size of an empty string in C, the values differ. Why is that so?

char b[7];

printf("String Length: %d", strlen(b)); // Returns 22
printf("String Size: %d", sizeof(b));   // Returns 7
5
  • Because if the array does not have the static storage duration then the code snippet behavior is undefined because the array is not initialized. Commented Oct 30, 2019 at 14:40
  • 1
    Undefined behavior because the array is not initialized? Please post a Minimal, Complete, and Verifiable example. Your printf format doesn't contain conversion specifiers, so 22 nor 7 won't be printed. Commented Oct 30, 2019 at 14:40
  • 4
    The new code also invokes undefined behavior for type mismatch: strlen and sizeof returns size_t while %d requires int. Commented Oct 30, 2019 at 14:43
  • 1
    The array is not "empty", it is unitialised. Commented Oct 30, 2019 at 16:33
  • This question should motivate more why both functions are expected to return the same value here? Commented Oct 30, 2019 at 16:38

4 Answers 4

3

This is due to the fact that the allocated space of your array isn't cleared. So on the stack where you char b[7]; is placed there is random data. The strlen() function starts on you the memory location b[0] to search for an end character '\0'.

In your case the first '\0' character is found and the 23rd location resulting in a length of 22. This number of 22 is undefined behaviour and can be different everytime you run your code.

If you initialize your array with '\0' at b[0] it will return a length of 0.

Sign up to request clarification or add additional context in comments.

Comments

3

The reason why strlen and sizeof gives different results is that the sizeof operator gives you the size of the array itself (in bytes) while the strlen function counts the number of "characters" until it finds a string null-terminator.

Even if you initialized the array (like e.g. char b[7] = "a";) the strlen function would give a different result from the sizeof operator.

There's simply no way you can make both give the same result without having undefined behavior one way or another.


Now as for why strlen returns 22 in your case, it's because uninitialized local variables, even arrays, really are uninitialized. Their values and contents are indeterminate and could be seen as random or garbage. Your array just happen to not have a random null-terminator inside it.

When you pass a pointer to the first element of the array to the strlen function, it will continue to look for the null-terminator even if it goes out of bounds. And in your specific case there just happens to be a byte corresponding to the null-terminator after strlen have counted 22 "random" or "garbage" characters.

Comments

0

When returning the length and the size of an empty string in C, the values differ. Why is that so?

If the array does not have the static storage duration then it does not contain a string even empty. It is just uninitialized and has indeterminate value. So using the function strlen results in undefined behavior.

Pay attention to that these calls

printf("String Length: %d", strlen(b));
printf("String Size: %d", sizeof(b));

have undefined behavior because there are used incorrect conversion specifiers. You have to use %zu instead of %d.

You could declare the array like

char b[7] = "";

or like

char b[7] = { '\0' };

or even like

char b[7] = { [6] = '\0' };

In these cases the array indeed contains an empty string and the function strlen will return 0.

In any case the function strlen and the operator sizeof return different values because the function strlen does not count the terminating zero of a string stored in an array.

Consider the following demonstrative program

#include <stdio.h>
#include <string.h>

int main(void) 
{
    char s[] = "Hello";

    printf( "strlen( s ) = %zu\n", strlen( s ) );
    printf( "sizeof( s ) = %zu\n", sizeof( s ) );

    return 0;
}

Its output is

strlen( s ) = 5
sizeof( s ) = 6

Comments

0

One [fixed-size] array can hold different length strings (at different times, of course)

char t[10]; x = sizeof t; /* 10 */
x = strlen(t); /* BANG: t  is not valid */
t[0] = '\0'; x = strlen(t); /* 0 */
strcpy(t, "1"); x = strlen(t); /* 1 */
strcpy(t, "12345678"); x = strlen(t); /* 8 */
strcpy(t, "123456789"); x = strlen(t); /* 9 */
strcpy(t, "1234567890"); /* BANG */

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.