C program - size of string with characters \ 0
sizeof
does not determine the length of the string. It determines how many bytes the structure occupies in memory.
In your case, a structure str
, an array of bytes. The compiler knows how many bytes, including the two trailing '\0'
s, were placed into the array, so it creates the correct size at compile time. sizeof
has no idea what str
a null terminated string C is, so it produces 15.
This is different from strlen
which interprets your string as a C string and returns the number of characters before the first '\0'
.
source to share