1

Sometimes I am using sequences of characters (strings) except the null terminator is not needed or wanted, for example if I am using memcpy() and the length is already known. A such, I prefer to omit the null terminator. A cumbersome way to do this would be declaring an array:

char no_term[5] = {'h', 'e', 'l', 'l', 'o'};

However, I would prefer to use quoted strings, as these are much more efficient to program with. However quoted strings automatically include a null terminator at the end. But would specifying the array size to exclude the null terminator invoke undefined behavior? Is the following valid C, as long as I do not use these where a null terminated string is required (e.g., passing them to strlen())?

char no_term[5] = "hello";
char no_term_array[3][3] = {"foo", "bar", "baz"};
12
  • 2
    Code such as char no_term[5] = "hello"; will get you whatever "You broke the build!" award/hat/dunce cap often enough your org will engrave your name on it. Commented Sep 4, 2022 at 22:41
  • 1
    One drawback to this is that humans are fallible, and counting characters is tedious and error prone. What is the size (without '\0') of "Supercalifragilisticexpialidotious" ?(sp??) Commented Sep 4, 2022 at 23:32
  • 1
    @Fe2O3 How about a macro function? #define NO_NULL_TERM(string_literal) (const char[sizeof(string_literal)-1]){string_literal} Commented Sep 4, 2022 at 23:33
  • 1
    @user16217248 Or a VLA perhaps... (Can't try it... Old compiler...) :-) Commented Sep 4, 2022 at 23:35
  • 2
    @Fe2O3 How would a zero length array compound literal behave? I do not know Commented Sep 4, 2022 at 23:42

1 Answer 1

6

According to §6.7.9 ¶14 of the ISO C11 standard, arrays of character type may be initialized from a string literal, even if there is no room for the terminating null character.

So yes, your posted code is valid and will not invoke undefined behavior.

Note however that this is only legal in C, not C++. In C++, there must be room for a terminating null character.

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

5 Comments

But the resultant variable is not a C string, because it has no NUL terminator. It's just an array, and it is undefined behaviour to handle it as if it is a string.
@WeatherVane That is why I said 'character array' in the title instead of 'string'
Yes, it is strictly true that the posted code will not be undefined behaviour but is slightly misleading, hence the comment.
@WeatherVane the question specifically asks for an array that will not be handled like a null-terminated string
It would have been nice if the Standard had provided a syntax to indicate whether the contents of a string literal were expected to completely fill a space, be exactly one byte shorter than a space, be at least one byte shorter than a space, or have an arbitrary size relationship with a space. In many cases, a programmer will expect one of those things, and it would be useful to have a diagnostic any time the expectation is violated.

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.