51

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
  • 601,492
  • 42
  • 1,072
  • 1,490
xiaokaoy
  • 1,608
  • 3
  • 15
  • 27
  • 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

2 Answers2

77

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
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 'So, the array a contains 5 elements.' Do you mean `4` elements? – Bikal Lem Feb 29 '16 at 02:03
  • 29
    @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
0

To point out some nuances related to C strings.

The char array sizeof will be 5, but the string will normally be "seen" as 3 chars + 1 null terminator. The extra null terminator won't be seen.

This is because strings are walked until the FIRST null terminator is encountered. This is why strlen will be 3 and not 4. The 3 letters are counted, and when it hits the null terminator that signifies the end of the string, so it stops.

When passing the char[] to a function, it will decay into a char*, so the fact that the original char[] was a size of 5 is further lost.

HOWEVER...if you passed the sizeof(a) into a function, then the extra null could cause issues, and of course should not be included in the string literal.

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

void main() {
    char a[] = "abc\0";
    printf("sizeof: %lu\n", sizeof(a));
    printf("strlen: %lu\n", strlen(a));
}

Output:

sizeof: 5
strlen: 3
Despertar
  • 21,627
  • 11
  • 81
  • 79