Namespaces
Variants
Views
Actions

string literal

From cppreference.com
< cpp‎ | language
Revision as of 01:05, 12 December 2012 by Bazzy (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Contents

Syntax

" (Template:sparam|Template:sparam)* " (1)
L " (Template:sparam|Template:sparam)* " (2)
u8 " (Template:sparam|Template:sparam)* " (3) (since C++11)
u " (Template:sparam|Template:sparam)* " (4) (since C++11)
U " (Template:sparam|Template:sparam)* " (5) (since C++11)
Template:sparam(optional) R "Template:sparam( Template:sparam* )Template:sparam" (6) (since C++11)

Explanation

unescaped_character - Any valid character
escaped_character - See escape sequences
prefix - One of L, u8, u, U
delimiter - A string made of any source character but parentheses, backslash and spaces (can be empty)
raw_character - Must not contain the closing sequence )Template:sparam"


1) Narrow multibyte string literal. The type of an unprefixed string literal is const char[]
2) Wide string literal. The type of a L"..." string literal is const wchar_t[]
3) UTF-8 encoded string literal. The type of a u8"..." string literal is const char[]
4) UTF-16 encoded string literal. The type of a u"..." string literal is const char16_t[]
5) UTF-32 encoded string literal. The type of a U"..." string literal is const char32_t[]
6) Raw string literal. Used to avoid escaping of any character, anything between the delimiters becomes part of the string, if Template:sparam is present has the same meaning as described above.

Notes

  • String literals can be concatenated
  • The null character ('\0', L'\0', char16_t(), etc) is always appended to the string literal: thus, a string literal "Hello" is a const char[6] holding the characters 'H', 'e', 'l', 'l', 'o', and '\0'.
  • String literals can be used to initialize character arrays.

Example

char array[] = "Foo" "bar";
// same as
char array[] = { 'F', 'o', 'o', 'b', 'a', 'r', '\0' };
 
const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char s2 = "\nHello\nWorld\n";