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
715
votes
8 answers

Insert text with single quotes in PostgreSQL

I have a table test(id,name). I need to insert values like: user's log, 'my user', customer's. insert into test values (1,'user's log'); insert into test values (2,''my users''); insert into test values (3,'customer's'); I am getting an error…
MAHI
  • 9,263
  • 11
  • 36
  • 47
549
votes
10 answers

C++ multiline string literal

Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.
rlbond
  • 65,341
  • 56
  • 178
  • 228
467
votes
7 answers

How to add percent sign to NSString

I want to have a percentage sign in my string after a digit. Something like this: 75%. How can I have this done? I tried: [NSString stringWithFormat:@"%d\%", someDigit]; But it didn't work for me.
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
371
votes
9 answers

How to split a string literal across multiple lines in C / Objective-C?

I have a pretty long sqlite query: const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC"; How can I break it in a number of lines to make it easier to…
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
271
votes
5 answers

How should I write a Windows path in a Python string literal?

Suppose I need to refer to the path C:\meshes\as. If I try writing that directly, like "C:\meshes\as", I encounter problems - either some exception, or the path just doesn't work. Is this because \ is acting as an escape character? How should I…
Gareth
  • 3,502
  • 4
  • 20
  • 21
259
votes
15 answers

Single quotes vs. double quotes in C or C++

When should I use single quotes and double quotes in C or C++ programming?
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
259
votes
13 answers

Difference between string object and string literal

What is the difference between String str = new String("abc"); and String str = "abc";
user395617
  • 2,635
  • 3
  • 16
  • 6
218
votes
19 answers

python: SyntaxError: EOL while scanning string literal

I have the above-mentioned error in s1="some very long string............" Does anyone know what I am doing wrong?
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
190
votes
8 answers

String literals: Where do they go?

I am interested in where string literals get allocated/stored. I did find one intriguing answer here, saying: Defining a string inline actually embeds the data in the program itself and cannot be changed (some compilers allow this by a smart trick,…
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
135
votes
3 answers

What is the r#""# operator in Rust?

I saw the operator r#"" in Rust but I can't find what it does. It came in handy for creating JSON: let var1 = "test1"; let json = r#"{"type": "type1", "type2": var1}"#; println!("{}", json) // => {"type2": "type1", "type2": var1} What's the name of…
Incerteza
  • 32,326
  • 47
  • 154
  • 261
110
votes
5 answers

How to use Macro argument as string literal?

I am trying to figure out how to write a macro that will pass both a string literal representation of a variable name along with the variable itself into a function. For example given the following function. void do_something(string name, int…
Ian
  • 4,169
  • 3
  • 37
  • 62
103
votes
4 answers

How to define string literal union type from constants in Typescript

I know I can define string union types to restrict variables to one of the possible string values: type MyType = 'first' | 'second' let myVar:MyType = 'first' I need to construct a type like that from constant strings, e.g: const MY_CONSTANT =…
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
103
votes
8 answers

Computing length of a C string at compile time. Is this really a constexpr?

I'm trying to compute the length of a string literal at compile time. To do so I'm using following code: #include int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf("%d %d",…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
101
votes
9 answers

"Life-time" of a string literal in C

Wouldn't the pointer returned by the following function be inaccessible? char *foo(int rc) { switch (rc) { case 1: return("one"); case 2: return("two"); default: …
user113454
  • 2,273
  • 9
  • 28
  • 35
94
votes
4 answers

Why do (only) some compilers use the same address for identical string literals?

https://godbolt.org/z/cyBiWY I can see two 'some' literals in assembler code generated by MSVC, but only one with clang and gcc. This leads to totally different results of code execution. static const char *A = "some"; static const char *B =…
1
2 3
79 80