What is the difference between 'x'
and "x"
?
Does 'x'
mean it is a char
value and "x"
mean it is a string value?
very sorry for the similarity to the other qn as I don't really get the explanation over there as it is too complicated.
What is the difference between 'x'
and "x"
?
Does 'x'
mean it is a char
value and "x"
mean it is a string value?
very sorry for the similarity to the other qn as I don't really get the explanation over there as it is too complicated.
The literal 'x'
is a char
. The literal "x"
is a string literal of type const char[2]
, a null-terminated char array holding values x
and \0
.
your assumption is correct,
"x" is a string
'x' is a char
'x' means a char with value 'x'.
"x" means a c-type const char array with value {'x', 0}
In C and C++, "x" is of type const char[]
which is an array and it is null-terminated (0x00). Whilst 'x' is of type char
.
The word 'string' is a little bit ambiguous, because it can mean two things -
const char[]
)std::string
. C++ has support in the standard library for the String datatype, which can be assigned a const char[]
.Just to clarify:
"I am"
Is actually this:
{'I', ' ', 'a', 'm', '\0'}