5

Here is a snippet of C99 code:

int main(void)
{
    char c[] = "\0";
    printf("%d %d\n", sizeof(c), strlen(c));

    return 0;
}

The program is outputting 2 0. I do not understand why sizeof(c) implies 2 seeing as I defined c to be a string literal that is immediately NULL terminated. Can someone explain why this is the case? Can you also provide a (some) resource(s) where I can investigate this phenomenon further on my own time.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
avinashse
  • 1,440
  • 4
  • 30
  • 55
  • 1
    read [What does sizeof(&arr) return?](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-return/15177499#15177499) – Grijesh Chauhan Aug 14 '13 at 07:36
  • 1
    remember `strlen()` - gives number of chars till first nul – Grijesh Chauhan Aug 14 '13 at 07:40
  • Also read: [5.2.1.2 Character sets ©ISO/IEC ISO/IEC 9899:201x](https://is.muni.cz/www/408176/38744863/International_Standard_for_C.txt) : In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or by escape sequences consisting of the backslash \ followed by one or more characters. A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string. – Grijesh Chauhan Aug 14 '13 at 07:54

6 Answers6

18

didn't understand why size of is showing 2.

A string literal has an implicit terminating null character, so the ch[] is actually \0\0, so the size is two. From section 6.4.5 String literals of the C99 standard (draft n1124), clause 5:

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals

As for strlen(), it stops counting when it encounters the first null terminating character. The value returned is unrelated to the sizeof the array that is containing the string. In the case of ch[], zero will be returned as the first character in the array is a null terminator.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • You should also say that strlen is 0 because the first character is null – SheetJS Aug 14 '13 at 07:32
  • I agree with you that string has implicit terminating null character.If ch is `"\0\0"` then strlen(ch) should be 1, but it is 0 – avinashse Aug 14 '13 at 07:33
  • `strlen` stops when it encounters `\0`, which is the first character of `ch`. Why should it return `1` ? – Nbr44 Aug 14 '13 at 07:34
  • @Nirk is it so that strlen checks and counts lenght only upto null character ? – avinashse Aug 14 '13 at 07:34
  • @avinashse - No, `strlen` looks for the first `'\0'` character, which in this case is _before_ the implicit nul-terminator. Try: `strlen("test\0this")`. – Chris Lutz Aug 14 '13 at 07:35
3

In C, "" means: give me a string and null terminate it for me.

For example arr[] = "A" is completely equivalent to arr[] = {'A', '\0'};

Thus "\0" means: give me a string containing a null termination, then null terminate it for me.

arr [] = "\0"" is equivalent to arr[] = {'\0', '\0'};

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

"\0" is not the same as "". String literals are nul-terminated, so the first is the same as the compound literal (char){ 0, 0 } whereas the second is just (char){ 0 }. strlen finds the first character to be zero, so assumes the string ends. That doesn't mean the data ends.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
2

When you declare a string literal as :

char c[]="\0";

It already has a '\0' character at the end so the sizeof(c) gives 2 because your string literal is actually : \0\0.

strlen(c) still gives 0 because it stops at the first \0.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
1

strlen measures to the first \0 and gives the count of characters before the \0, so the answer is zero

sizeof on a char x[] gives the amount of storage used in bytes which is two, including the explict \0 at the end of the string

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
0

Great question. Consider this ...

ubuntu@amrith:/tmp$ more x.c
#include <stdio.h>
#include <string.h>

int main() {
  char c[16];
  printf("%d  %d\n",sizeof(c),strlen(c));
  return 0;
}
ubuntu@amrith:/tmp$ ./x
16  0
ubuntu@amrith:/tmp$

Consider also this:

ubuntu@amrith:/tmp$ more x.c
#include <stdio.h>
#include <string.h>

int main() {
  int c[16];
  printf("%d\n",sizeof(c));
  return 0;
}
ubuntu@amrith:/tmp$ ./x
64
ubuntu@amrith:/tmp$

When you initialize a variable as an array (which is effectively what c[] is), sizeof(c) will give you the allocated size of the array.

The string "\0" is the literal string \NUL\NUL which takes two bytes.

On the other hand, strlen() computes the string length which is the offset into the string of the first termination character and that turns out to be zero and hence you get 2, 0.

amrith
  • 953
  • 6
  • 17