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
25
votes
2 answers

How to create a formatted String out of a literal in Rust?

I'm about to return a string depending the given argument. fn hello_world(name:Option) -> String { if Some(name) { return String::formatted("Hello, World {}", name); } } This is a not available associated function! - I…
xetra11
  • 7,671
  • 14
  • 84
  • 159
25
votes
3 answers

Differentiate String Literal from Char Array

I want to write some function that takes a string literal - and only a string literal: template void foo(const char (&str)[N]); Unfortunately, that is too expansive and will match any array of char - whether or not it's a true string…
Barry
  • 286,269
  • 29
  • 621
  • 977
24
votes
4 answers

Where does const char* get the pointer to a memory address?

This may be simple question, but why does a const char* not need a memory address to point to? Example: const char* a = "Anthony"; and not: const char *a = // Address to const char like any other types do?
Weidelix
  • 401
  • 3
  • 6
24
votes
2 answers

Does C support raw string literals?

C++11 added support for raw string literals, such as: R"foo(A " weird \" string)foo" Does C have such a thing? If so, in what version of the standard? C11? If not, does anyone know if it is being planed and if any compilers support it?
Baruch
  • 20,590
  • 28
  • 126
  • 201
24
votes
6 answers

What is the backslash character (\\)?

What is the string literal \\ backslash? What does it do? I have thought about it but I do not understand it. I also read it on wikipedia. When I try to print the following: System.out.println("Mango \\ Nightangle"); the output is: Mango \…
Y.E.P
  • 1,187
  • 6
  • 20
  • 40
23
votes
3 answers

Some const char * are unavailable at compile time?

Let's suppose we have a template function with non-type parameter of const char * like this: template void print() { std::cout << MESSAGE << '\n'; } Using this template wouldn't be a problem as log as the MESSAGE can be…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
22
votes
2 answers

Why does std::views::split() compile but not split with an unnamed string literal as a pattern?

When std::views::split() gets an unnamed string literal as a pattern, it will not split the string but works just fine with an unnamed character literal. #include #include #include #include #include…
khaos
  • 632
  • 3
  • 11
22
votes
9 answers

If char*s are read only, why can I overwrite them?

My course taught me that char*s are static/read only so I thought that would mean you can't edit them after you have defined them. But when I run: char* fruit = "banana"; printf("fruit is %s\n", fruit); fruit = "apple"; printf("fruit is %s\n",…
sally2000
  • 788
  • 4
  • 14
22
votes
1 answer

C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

I am using gnuplot to draw a graph in C++. The graph is being plot as expected but there is a warning during compilation. What does the warning mean? warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] This is the…
Sagar
  • 273
  • 1
  • 3
  • 12
21
votes
1 answer

Kotlin - Form feed character - Illegal escape: '\f'

Kotlin does not support the escape "\f" (Form Feed Character). So what is the proper way port "\f" from java to Kotlin? Java: String str = "\f"; // OK Kotlin: var str = "\f" // Illegal escape: '\f' Anyway, that looked like a bug to me because…
damphat
  • 18,246
  • 8
  • 45
  • 59
21
votes
3 answers

MySQL unicode literals

I want to insert a record into MySQL that has a non-ASCII Unicode character, but I'm on a terminal that doesn't let me easily type non-ASCII characters. How do I escape a Unicode literal in MySQL's SQL syntax?
Ken
  • 316
  • 2
  • 7
21
votes
4 answers

Does Haskell concatenate String literals at compile time?

Does Haskell 2010 guarantee to concatenate String literals at compile time? If I have "This is a " ++ "very long String that " ++ "spans several lines" does the compiler treat it as "This is a very long String that spans several lines" I want to…
Ralph
  • 31,584
  • 38
  • 145
  • 282
21
votes
3 answers

auto with string literals

#include #include int main() { const char a[] = "hello world"; const char * p = "hello world"; auto x = "hello world"; if (typeid(x) == typeid(a)) std::cout << "It's an array!\n"; else if…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
20
votes
2 answers

String Literal Differences Between C and C++

As far as I can tell, before C++11, string literals were handled in almost exactly the same way between C and C++. Now, I acknowledge that there are differences between C and C++ in the handling of wide string literals. The only differences that I…
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
20
votes
6 answers

C++: Constructor accepting only a string literal

Is it possible to create a constructor (or function signature, for that matter) that only accepts a string literal, but not an e.g. char const *? Is it possible to have two overloads that can distinguish between string literals and char const *? C++…
peterchen
  • 40,917
  • 20
  • 104
  • 186