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
19
votes
3 answers

Type id of std::string for variable vs. string in argument?

I referred to http://en.cppreference.com/w/cpp/language/typeid to write code which does different things for different types. The code is as below and the explanation is given in the comments. #include #include using namespace…
Meehatpa
  • 349
  • 2
  • 10
19
votes
1 answer

Difference between string literal and constexpr array of char

I have been wondering if there is any difference between what is being pointed by ptrToArray and ptrToLiteral in the following example: constexpr char constExprArray[] = "hello"; const char* ptrToArray = constExprArray; const char* ptrToLiteral =…
jms
  • 719
  • 2
  • 8
  • 18
19
votes
6 answers

Searching For String Literals

In the quest for localization I need to find all the string literals littered amongst our source code. I was looking for a way to script this into a post-modification source repository check. (I.E. after some one checks something in have a box setup…
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
19
votes
4 answers

Why does gcc allow char array initialization with string literal larger than array?

int main() { char a[7] = "Network"; return 0; } A string literal in C is terminated internally with a nul character. So, the above code should give a compilation error since the actual length of the string literal Network is 8 and it cannot…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
18
votes
2 answers

Why are string literals &str instead of String in Rust?

I'm just asking why Rust decided to use &str for string literals instead of String. Isn't it possible for Rust to just automatically convert a string literal to a String and put it on the heap instead of putting it into the stack?
18
votes
2 answers

Why don't C compilers warn about incompatible types with literal strings?

The following program provokes systematic segmentation fault due to undefined behavior (trying to modify a string literal): int main() { char *s = "immutable"; s[0] = 'a'; return 0; } Still, there seems to be absolutely no way to tell…
anol
  • 8,264
  • 3
  • 34
  • 78
18
votes
1 answer

Single vs Double quotes in Julia

What is the difference between single quotes and double quotes in Julia? Unlike Python, for strings, it doesn't allow single quotes: > s = 'abc' syntax: invalid character literal > s = "abc" > print(s) abc But when trying to single quote a double…
alvas
  • 115,346
  • 109
  • 446
  • 738
18
votes
5 answers

Macro for static std::string object from literal

Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code: int foo(const std::string&); .. foo("bar"); .. foo("baz"); Calling a function with a string literal like this will create…
Masseman
  • 359
  • 3
  • 13
18
votes
1 answer

String literals: pointer vs. char array

In this statement: char *a = "string1" What exactly is string literal? Is it string1? Because this thread What is the type of string literals in C and C++? says something different. Up to my knowledge int main() { char *a = "string1"; //is a…
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
17
votes
2 answers

Can a string literal be subscripted in a constant expression?

This is valid, because a constexpr expression is allowed to take the value of "a glvalue of literal type that refers to a non-volatile object defined with constexpr, or that refers to a sub-object of such an object" (§5.19/2): constexpr char str[] =…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
17
votes
1 answer

Efficient C++ way of giving literal meaning to special symbols (") in a C++ string

I want to put this: