49

For example:

char a[] = "abc\0";

Does standard C say that another byte of value 0 must be appended even if the string already has a zero at the end? So, is sizeof(a) equal to 4 or 5?

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
xiaokaoy
  • 1,568
  • 3
  • 13
  • 26
  • 1
    There absolutely nothing wrong with the English in your question. But couldn't you find the answer by simply trying it? – Barmar Jul 30 '13 at 09:41
  • 3
    If you want to be explicit, you could write: `char a[] = {'a','b','c','\0'};`. This isn't declared as a string literal so an extra terminating null isn't appended. – Coder_Dan Jul 30 '13 at 10:10
  • Alternatively, you could write `char a[4] = "abc\0";`. – nwellnhof Dec 23 '16 at 11:22
  • The latter might seem kind of wrong because the standard says an additional '\0' is appended making the string literal 5 chars in size and thus seemingly too large for a 4-char array. However, in the case an initializer is too large for a fixed-size array the surplus elements are simply ignored/not used for initialization (§6.7.8 paragraph 14) which is OK in this case but I would avoid writing it like that. – stefanct Feb 04 '18 at 18:47

1 Answers1

74

All string literals have an implicit null-terminator, irrespective of the content of the string.

The standard (6.4.5 String Literals) says:

A byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.

So, the string literal "abc\0" contains the implicit null-terminator, in addition to the explicit one. So, the array a contains 5 elements.

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
  • 'So, the array a contains 5 elements.' Do you mean `4` elements? – Bikal Lem Feb 29 '16 at 02:03
  • 27
    @BikalGurung: No, 5 is correct. {'a', 'b', 'c', '\0', '\0'} – Dietrich Epp Feb 29 '16 at 02:11
  • There's an exception to the implicit null terminator in C, e.g. `char s[3] = "abc";` here, `s` will not have a null terminator. (BTW this will be a compile error in C++). See [this answer](https://stackoverflow.com/a/14884549/6306190). – johan Jan 23 '22 at 23:33