-2

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.

Isabella Chan
  • 41
  • 1
  • 10

4 Answers4

10

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

your assumption is correct,

"x" is a string
'x' is a char
Snappawapa
  • 1,697
  • 3
  • 20
  • 42
  • 3
    I would be careful calling it a string in C++. It is a CString (null terminated array of characters) not an std::string. The fact you can assign one to the other doesn't mean they are the same. – harmic Mar 07 '14 at 06:22
  • 1
    @harmic Even [CString](http://msdn.microsoft.com/en-us/library/aa300688%28v=vs.60%29.aspx) is confusing, because there is a class out there with that name. – juanchopanza Mar 07 '14 at 06:28
  • Basically, a char value is just a char value. A string is simply an array of chars, ended with a null terminator. There are different implementations of this idea, I.E. CString and std::string, but the basic idea is the same – Snappawapa Mar 07 '14 at 06:38
  • @juanchopanza yeah I forgot that one. Ok, "C style string". – harmic Mar 07 '14 at 06:40
  • "There are different implementations of this idea". No, `"x"` is neither an `std::string` or a CString`. It is a string literal, and it can only be of one type. – juanchopanza Mar 07 '14 at 06:44
  • Well yea it can only have one type, I'm just saying that the way the chars are stored are essentially the same. – Snappawapa Mar 07 '14 at 06:47
0

'x' means a char with value 'x'.

"x" means a c-type const char array with value {'x', 0}

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
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 -

  • A string of characters (const char[])
  • The datatype 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'}

Ospho
  • 2,756
  • 5
  • 26
  • 39