Questions tagged [string-literals]

String literals concern the syntactic representation of literal constant strings in C, C++ and others.

C and C++ have several ways to represent strings in code. They include the basic double quoted "Hello, World!". Strings can be concatenated "as" " follows". C++-0X has added new encodings u"Hello" for UTF16 (char16_t), U"world" for UTF32 (char32_t) is addition to L"long" for wchar_t. There are rules for concatenating different encodings.

1193 questions
53
votes
2 answers

How can I get double quotes into a string literal?

I have the following output created using a printf() statement: printf("She said time flies like an arrow, but fruit flies like a banana."); but I want to put the actual quotation in double-quotes, so the output is She said "time flies like an…
kevthanewversi
  • 3,686
  • 4
  • 27
  • 27
52
votes
2 answers

Why do string literals (char*) in C++ have to be constants?

I've recently been learning C++ and have realised that string literals in C++ have to be constants, whereas in C, they do not. Here is an example. The following code would be valid in C, but not in C++: char* str = "Hello, World!"; In order to do…
Serket
  • 3,785
  • 3
  • 14
  • 45
50
votes
3 answers

Include )" in raw string literal without terminating said literal

The two characters )" terminate the raw string literal in the example below. The sequence )" could appear in my text at some point, and I want the string to continue even if this sequence is found within it. R"( Some Text)" )"; //…
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
46
votes
1 answer

Why does the compiler prefer f(const void*) to f(const std::string &)?

Consider the following piece of code: #include #include // void f(const char *) { std::cout << "const char *"; } // <-- comment on purpose void f(const std::string &) { std::cout << "const std::string &"; } void f(const void *)…
45
votes
3 answers

Should I compare a std::string to "string" or "string"s?

Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first…
andreee
  • 4,459
  • 22
  • 42
43
votes
2 answers

What's the "E" before a Postgres string?

I was reading a Postgres/PostGIS statement like this: SELECT ST_AsBinary( ST_GeomFromWKB( E'\\001\\001\\000\\000\\000\\321\\256B\\312O\\304Q\\300\\347\\030\\220\\275\\336%E@', 4326 ) ); The above creates something from a Well Known Binary…
thor
  • 21,418
  • 31
  • 87
  • 173
43
votes
2 answers

Restricting string literals to Text only

I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text, and…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
42
votes
10 answers

Keeping code structure with string literal that uses whitespace

So a bit of a weird question I was having trouble coming up with the search terms for. If I have a multi-line string literal in my program, is there anyway to keep the indentation of my code consistent without adding unwanted white space to my…
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
40
votes
4 answers

shared c constants in a header

I want to share certain C string constants across multiple c files. The constants span multiple lines for readability: const char *QUERY = "SELECT a,b,c " "FROM table..."; Doing above gives redefinition error for QUERY. I don't…
Manish
  • 1,985
  • 2
  • 20
  • 29
39
votes
6 answers

String literals not allowed as non type template parameters

The following quote is from C++ Templates by Addison Wesley. Could someone please help me understand in plain English/layman's terms its gist? Because string literals are objects with internal linkage (two string literals with the same value but in…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
39
votes
4 answers

How to make Jade stop HTML encoding element attributes, and produce a literal string value?

UPDATE Jade v0.24.0 fixes this with a != syntax for attributes. option(value!='<%= id %>') I'm trying to build an
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
38
votes
6 answers

Inconsistency between std::string and string literals

I have discovered a disturbing inconsistency between std::string and string literals in C++0x: #include #include int main() { int i = 0; for (auto e : "hello") ++i; std::cout << "Number of elements: " << i <<…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
38
votes
5 answers

What does %S mean in PHP, HTML or XML?

I'm looking at Webmonkey's PHP and MySql Tutorial, Lesson 2. I think it's a php literal. What does %s mean? It's inside the print_f() function in the while loops in at least the first couple of code blocks. printf("%s…
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
37
votes
1 answer

What is the result of decltype("Hello")?

I'm getting unexpected results from all compilers on which I tried the following (GCC 4.7.2, GCC 4.8.0 beta, ICC 13.0.1, Clang 3.2, VC10): #include int main() { // This will fire static_assert( …
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
37
votes
4 answers

Is the u8 string literal necessary in C++11

From Wikipedia: For the purpose of enhancing support for Unicode in C++ compilers, the definition of the type char has been modified to be at least the size necessary to store an eight-bit coding of UTF-8. I'm wondering what exactly this means for…
Lukas Schmelzeisen
  • 2,934
  • 4
  • 24
  • 30
1 2
3
79 80