sizeof("hello")

Please explain me why sizeof("hello") return value 6. I think the string length of hello is only 5. Thank in advance!

cout << sizeof("hello");
The '\0' string terminator on the end of a char array is part of the array.
If you did the following you'd get 5.
1
2
std::string str("hello");
cout << str.size() << endl;
Just to add a bit,

sizeof is not the same thing as string length.

The literal "hello" is of type char[6] because, as janra said, the null terminator is implicitly included.

Topic archived. No new replies allowed.