What is the datatype of string literal in C++?

It is a const char[N] (which is the same thing as char const[N]), where N is the length of the string plus one for the terminating NUL (or just the length of the string if you define “length of a string” as already including the NUL).

This is why you can do sizeof("hello") - 1 to get the number of characters in the string (including any embedded NULs); if it was a pointer, it wouldn’t work because it would always be the size of pointer on your system (minus one).

Leave a Comment