2

I was trying different ways to declare a string in C for exam preparation. We know that string in C is character array with '\0' at end. Then I found that even if I declare an array of 5 characters and put 5 characters in it like "abcde" it is accepted. Then where is the null char stored?

I declared strings in following ways

char str[] = {'a','b','c','d','e'};
char str2[] = "abcde";
char str3[5] = "abcde";

Now, in the 3rd case, I am allocating 5 byte of space and I have exactly 5 characters in the array, then if string should have a null character at end where is it being stored? Or is it the case that null is not appended? What about the 1st and 2nd cases, are null appended there? strlen() returns 5 in all 3 cases.

1
  • 1
    Maybe consider another case: char str4[] = "abc\0\0";. sizeof(str4) --> 6 and strlen(str4) --> 3. Commented Apr 2, 2019 at 12:40

2 Answers 2

6

The NUL character is stored only in the second example

char str2[] = "abcde";

where the array is sized automatically to include it. An array of characters does not have to be a string, and the other two are encoded without the NUL terminator.

If the code happens to treat them correctly as strings, that was an unfortunate result of undefined behaviour.

#include <stdio.h>

int main(int argc, char *argv[])
{
    char str[] = {'a','b','c','d','e'};
    char str2[] = "abcde";
    char str3[5] = "abcde";
    printf("%zu\n", sizeof str);
    printf("%zu\n", sizeof str2);
    printf("%zu\n", sizeof str3);
}

Program output:

5
6
5

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

Comments

2

In the third case, the null terminator is not appended. From the documentation:

If the size of the array is known, it may be one less than the size of the string literal, in which case the terminating null character is ignored:

So when you check its length with strlen, you get undefined behavior (and the the same when you try it with the first one, since that also isn't a string).

Any less that that is not allowed, (for me on MSVC it shows an error but still compiles it, for some reason). More than the string length is allowed, in which case the rest is zero-initialized.

4 Comments

@Blaze Thanks. Just wanted to know, undefined behavior in strlen() as in it will return the length of actual string literal?
@KushalMondal it's undefined behavior in strlen because that's its contract, the pointer passed must be a valid null-terminated string. strlen just keeps reading until it finds a null-terminator, so if there's none in where it allowed to read, it might cause an access violation or something similar. For instance the result I got in your third case was 21.
The first case is also not a string.
@chux good point, I added that to the answer, as it previously may not have been obvious why it's undefined behavior with the first case.

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.