The sizeof
operator yields the size (in bytes) of its operand.
In this statement
char a[] = "Apple";
array a is initialized by characters of string literal "Apple" that includes the terminating zero.
In fact this record is equivalent to
char a[] = { 'A', 'p', 'p', 'l', 'e', '\0'; };
So the size in bytes of a is equal to 6.
Standard C function strlen
counts symbols in a string until it encounters the terminating zero. So
strlen( a )
will return 5 that is the number of characters in the array that are before the terminating zero.
Take into account that you could write for example
char a[100] = "Apple";
In this case sizeof( a )
will yield 100 because you explicitly specified the number of bytes that the array will occupy. However it was initialized only with 6 characters of the string literal. So how to find how many actual data are in the character array? For this purpose function strlen
was introduced that to distinguish the size of a character array and the number of actual data in the character array.