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
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.
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.
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
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 */
printfformat doesn't contain conversion specifiers, so22nor7won't be printed.strlenandsizeofreturnssize_twhile%drequiresint.