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
93
votes
12 answers

Using quotation marks inside quotation marks

When I want to do a print command in Python and I need to use quotation marks, I don't know how to do it without closing the string. For instance: print " "a word that needs quotation marks" " But when I try to do what I did above, I end up…
Thi G.
  • 1,578
  • 5
  • 16
  • 27
91
votes
1 answer

Unicode encoding for string literals in C++11

Following a related question, I'd like to ask about the new character and string literal types in C++11. It seems that we now have four sorts of characters and five sorts of string literals. The character types: char a = '\x30'; //…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
84
votes
3 answers

What is the rationale for parenthesis in C++11's raw string literals R"(...)"?

There is a very convenient feature introduced in C++11 called raw string literals, which are strings with no escape characters. And instead of writing this: regex mask("\\t[0-9]+\\.[0-9]+\\t\\\\SUB"); You can simply write this: regex…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
79
votes
5 answers

Why are string literals l-value while all other literals are r-value?

C++03 5.1 Primary expressions §2 says: A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are rvalues. Similarly, C99 6.5.1 §4 says: A string literal is a primary expression.…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
70
votes
7 answers

How do I encode Unicode character codes in a PowerShell string literal?

How can I encode the Unicode character U+0048 (H), say, in a PowerShell string? In C# I would just do this: "\u0048", but that doesn't appear to work in PowerShell.
dan-gph
  • 16,301
  • 12
  • 61
  • 79
64
votes
5 answers

what does cout << "\n"[a==N]; do?

In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout, but it does not print a newline when the value of a is equal to N.
Siva Prasad
  • 753
  • 5
  • 10
64
votes
8 answers

C++ Comparison of String Literals

I'm a c++ newbie (just oldschool c). My son asked for help with this and I'm unable to explain it. If he had asked me "how do I compare strings" I would have told him to use strcmp(), but that isn't what is confusing me. Here is what he…
simusid
  • 1,854
  • 3
  • 18
  • 26
63
votes
3 answers

How to "instanceof" a primitive string (string literal) in JavaScript

In JavaScript, I can declare a string in the following ways; var a = "Hello World"; var b = new String("Hello World"); but a is not an instance of String... console.log(a instanceof String); //false; console.log(b instanceof String); //true; So…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
62
votes
5 answers

Why are the memory addresses of string literals so different from others', on Linux?

I noticed that string literals have very different addresses in memory than other constants and variables (Linux OS): they have many leading zeroes (not printed). Example: const char *h = "Hi"; int i = 1; printf ("%p\n", (void *) h); printf ("%p\n",…
Noidea
  • 1,405
  • 11
  • 17
60
votes
7 answers

What does the symbol \0 mean in a string-literal?

Consider following code: char str[] = "Hello\0"; What is the length of str array, and with how much 0s it is ending?
UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
59
votes
6 answers

Does JavaScript have literal strings?

In C#, Ruby, and many other languages you can denote a string as to not need escaping. In C# it’s like this string s = @"\whatever\this\is"; The results are when printed: \whatever\this\is Is this supported in any form in JavaScript?
DevelopingChris
  • 39,797
  • 30
  • 87
  • 118
59
votes
6 answers

Regular expression for a string literal in flex/lex

I'm experimenting to learn flex and would like to match string literals. My code currently looks like: "\""([^\n\"\\]*(\\[.\n])*)*"\"" {/*matches string-literal*/;} I've been struggling with variations for an hour or so and can't get it…
Thomas
  • 929
  • 2
  • 9
  • 10
55
votes
4 answers

Are char arrays guaranteed to be null terminated?

#include int main() { char a = 5; char b[2] = "hi"; // No explicit room for `\0`. char c = 6; return 0; } Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us,…
Dan
  • 2,694
  • 1
  • 6
  • 19
55
votes
4 answers

Why are my two tuples containing strings, created the same way, not equal?

I'm compiling the following program using Microsoft Visual C++, as a C++20 program: #include #include int main() { auto t1 = std::make_tuple("one", "two", "three"); auto t2 = std::make_tuple("one", "two", "three"); …
AeroSun
  • 2,401
  • 2
  • 23
  • 46
53
votes
7 answers

C/C++, can you #include a file into a string literal?

I have a C++ source file and a Python source file. I'd like the C++ source file to be able to use the contents of the Python source file as a big string literal. I could do something like this: char* python_code = " #include "script.py" " But that…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
1
2
3
79 80