15

What is the difference between

char str1[32] = "\0";

and

char str2[32] = "";
prakash
  • 58,901
  • 25
  • 93
  • 115

4 Answers4

23

Since you already declared the sizes, the two declarations are exactly equal. However, if you do not specify the sizes, you can see that the first declaration makes a larger string:

char a[] = "a\0";
char b[] = "a";

printf("%i %i\n", sizeof(a), sizeof(b));

prints

3 2

This is because a ends with two nulls (the explicit one and the implicit one) while b ends only with the implicit one.

Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
17

Well, assuming the two cases are as follows (to avoid compiler errors):

char str1[32] = "\0";
char str2[32] = "";

As people have stated, str1 is initialized with two null characters:

char str1[32] = {'\0','\0'};
char str2[32] = {'\0'};

However, according to both the C and C++ standard, if part of an array is initialized, then remaining elements of the array are default initialized. For a character array, the remaining characters are all zero initialized (i.e. null characters), so the arrays are really initialized as:

char str1[32] = {'\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0'};
char str2[32] = {'\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0',
                 '\0','\0','\0','\0','\0','\0','\0','\0'};

So, in the end, there really is no difference between the two.

  • 1
    This is a great answer. This made me also understand why `char a[2]="be"` would not be wise. – Pithikos Nov 07 '11 at 17:21
  • Are the elements also initialized to '\0' when allocating memory to a string through malloc? – Lokesh Oct 09 '14 at 03:31
  • @Lokesh: No — the memory allocated by `malloc()` is not initialized to any determinate value. It may be zeros when the memory is first allocated, but if it is modified, freed and then allocated a second time, the newly allocated space will (usually) have whatever values happened to be there when it was freed, unless `malloc()` used some of the space for its internal bookkeeping, etc. – Jonathan Leffler Oct 09 '14 at 14:53
2

As others have pointed out, "" implies one terminating '\0' character, so "\0" actually initializes the array with two null characters.

Some other answerers have implied that this is "the same", but that isn't quite right. There may be no practical difference -- as long the only way the array is used is to reference it as a C string beginning with the first character. But note that they do indeed result in two different memory initalizations, in particular they differ in whether Str[1] is definitely zero, or is uninitialized (and could be anything, depending on compiler, OS, and other random factors). There are some uses of the array (perhaps not useful, but still) that would have different behaviors.

Larry Gritz
  • 13,331
  • 5
  • 42
  • 42
  • In the context of the question, where the size of the array is specified, the results are identical — letter `'a'` followed by 31 null bytes, `'\0'`. Only in the context of an unsized array is there a difference. – Jonathan Leffler Oct 09 '14 at 04:02
0

Unless I'm mistaken, the first will initialize 2 chars to 0 (the '\0' and the terminator that's always there, and leave the rest untouched, and the last will initialize only 1 char (the terminator).

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319