-3

When I use :

    std::cout << "Hello world ";

Which type is "Hello world" ?

Where does it stored , so I can get it out and work with it ?

For some reasons, I don't want to use something like :

    std::string str = "Hello world"; 
    std::cout << str;

Please help me, I searched an hour but still no answer.

  • 3
    The type of a string literal in C++ is `const char[N]`. The other parts of your question are really unclear to me. – BlackDwarf Sep 29 '15 at 14:38
  • 2
    Using `std::string str = "Hello world";` is absolutely the best way to store `"Hello world"` and do work with it. – huu Sep 29 '15 at 14:39
  • The string constant gets placed into the data section of your compiled program. When the program is initialized, it gets loaded into memory on the heap (usually). – Shotgun Ninja Sep 29 '15 at 14:43
  • 1
    Also, what are "some reasons"? Why can't you declare a variable? The only thing I could think of is code golf, but if you're golfing you should probably know the language you're using better than this. – Shotgun Ninja Sep 29 '15 at 14:44
  • The base underlying type is `const char&`, if I'm not mistaken; a const reference to the region of memory containing the sequence of characters making up the string, terminated by a `'\0'` or NULL character. (It might be represented as a `const char*` or a `const char[N]`, I'm not sure, but the three types are *nearly* interchangeable.) – Shotgun Ninja Sep 29 '15 at 14:45

1 Answers1

0

The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084