This is most likely a very stupid question, but in C++, what is the data type for a string? I want to make the variable x = "Slim Shady".
Would I declare x as an int? I have tried declaring it as a char, but when I cout the variable, it only gives the first letter. Any help would be appreciated.
"Slim Shady" is constant literal of type
char[11]
which is convertible toconst char*
. You can also use it for initialization of any char arrays bigger than 11. See:See this little code:
You get (on gcc) mangled names:
From what I know - the first is
const char*
the second ischar[11]
. This is easy to explain since when passing array of anything to function - it is converted to const pointer.You need to include the header file string
Then you will be able to use std::string
http://en.cppreference.com/w/cpp/string/basic_string
You can also not use std::strings, if you dont want to use them althought they are your best option
You can use Char arrays or char pointer (C-strings)
or
The auto keyword is a C++0x feature. You should stay away from it till you learn the C++ basics
std::string
is the best bet. But you can also useauto
, as inauto x = "Slim Shady!";
. This means that you don't have to figure out the type to use the expression.Do you just mean: