#include
#include
#include
int main(void)
{
char qq[] = {\'a\' , \'b\' , \'c\' , \'d\'};
char qqq[] = \"abcd\";
In your code
char qq[] = {'a' , 'b' , 'c' , 'd'};
qq
is not qualified to be called a string, as it is not null-terminated. So calling strlen()
wilh qq
is inviting trouble.
What actually happens here is, strlen()
goes past the last valid element in qq
in search of terminating null (which is actually missing), and thus, venture into out-of-bound memory, which invokes undefined behaviour.
Solution: To make qq
a string, you have to add the null-terminator in the initializer list yourself, like
char qq[] = {'a' , 'b' , 'c' , 'd', '\0'};
That said, as I already mentioned in my comment, as strlen() return type is size_t
, you should use %zu
format specifier to print the result.
Just in a lighter mood, to answer
what should strlen() really return in this code?
If I say, "The phone number of the Respected President of India", well, technically, I might be correct !!
On a serious note, the output of UB in, well, undefined.