1

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.

Mohamed Ahmed Nabil
  • 3,949
  • 8
  • 36
  • 49
masonc15
  • 1,043
  • 2
  • 12
  • 19

4 Answers4

4

You need to include the header file string

#include <string>

Then you will be able to use std::string

std::string x="Slim Shady"

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)

char x[]="Slim Shady";

or

char* x="Slim Shady";

The auto keyword is a C++0x feature. You should stay away from it till you learn the C++ basics

Mohamed Ahmed Nabil
  • 3,949
  • 8
  • 36
  • 49
3

std::string is the best bet. But you can also use auto, as in auto x = "Slim Shady!";. This means that you don't have to figure out the type to use the expression.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 2
    *"This means that you don't have to figure out the type to use the expression."* - Until you need to do anything meaningful with it, in which case you'll need to know that the type is `char*` (or `const char*` depending on your compiler). – Ed S. Sep 16 '12 at 23:22
  • 1
    @EdS.: It *has* to be `char const*`. `char*` for a string literal is ill-formed. – Xeo Sep 16 '12 at 23:22
  • 1
    Dont make a beginner start using auto – Mohamed Ahmed Nabil Sep 16 '12 at 23:30
  • 1
    Instead he should use awful C array declarations, amirite? – Puppy Sep 16 '12 at 23:33
  • @Xeo: You're right; the type is an array of const char. However, the conversion from `const char[]` to `char*` is perfectly well defined, so it is not "ill-formed" as you say. Modifying the data is simply undefined behavior. – Ed S. Sep 16 '12 at 23:34
  • @DeadMG: The problem is that the use of `auto` assumes you know what you are doing. When you run into compilation errors due to type mismatches and everything is declared as `auto` you have no idea (as a beginner) as to what the hell is going on. One should have a firm understanding of the type system before mucking around with `auto`. – Ed S. Sep 16 '12 at 23:35
  • And, to elaborate; this response doesn't tell the OP what the type of a string literal is. It tells him/her to assign it to a string (which is a conversion) or use `auto` (which says nothing of the actual type). S/he still doesn't know what the type of `"hello"` is. – Ed S. Sep 16 '12 at 23:42
  • In this case, ignorance is bliss. – Puppy Sep 17 '12 at 00:00
2

"Slim Shady" is constant literal of type char[11] which is convertible to const char*. You can also use it for initialization of any char arrays bigger than 11. See:

char a[] = "Slim Shady";
char a[11] = "Slim Shady";
const char* a = "Slim Shady";
char a[12] = "Slim Shady";

See this little code:

#include <typeinfo>
#include <iostream>

template <class T>
void printTypeName(T t)
{
   std::cout << typeid(T).name() << std::endl;
}

int main() {
  printTypeName("Slim Shady");
  printTypeName<decltype("Slim Shady")>("Slim Shady");
}

You get (on gcc) mangled names:

PKc
A11_c 

From what I know - the first is const char* the second is char[11]. This is easy to explain since when passing array of anything to function - it is converted to const pointer.

Community
  • 1
  • 1
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
1

Do you just mean:

std::string x = "Slim Shady";
Tristan
  • 413
  • 1
  • 6
  • 10