In this statement:
char ch[] = "Hello";
A null terminated string literal is copied into ch
. Therefore there are six characters, including the NUL terminator. Note that strlen
will not count the NUL terminator.
char c[] = "Hello";
printf("%s", c);
Therefore, you should use strlen
when you want the size of the string, and sizeof
when you want the amount of bytes in the string. Note that in the case you have a character pointer, and not an array, it will have no size information.
char* ptr = "Hello";
sizeof(ptr); // size of a character pointer
sizeof(*ptr); // size of a char
strlen(ptr);