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
35
votes
4 answers

Lifetime of a string literal returned by a function

Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun(), where is "Some text!!" stored (I…
sud03r
  • 19,109
  • 16
  • 77
  • 96
34
votes
3 answers

How to declare constexpr C string?

I think i quite understand how to use the keyword constexpr for simple variable types, but i'm confused when it comes to pointers to values. I would like to declare a constexpr C string literal, which will behave like #define my_str "hello" That…
Youda008
  • 1,788
  • 1
  • 17
  • 35
31
votes
6 answers

How can Delphi 'string' literals have more than 255 characters?

I'm working with Delphi 7 and Strings, and I came across this: For a string of default length, that is, declared simply as string, max size is always 255. A ShortString is never allowed to grow to more than 255 characters. Once I had to do…
PresleyDias
  • 3,657
  • 6
  • 36
  • 62
31
votes
3 answers

Why is '\n' preferred over "\n" for output streams?

In this answer we can read that: I suppose there's little difference between using '\n' or using "\n", but the latter is an array of (two) characters, which has to be printed character by character, for which a loop has to be set up, which is more…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
31
votes
7 answers

Where do Java and .NET string literals reside?

A recent question about string literals in .NET caught my eye. I know that string literals are interned so that different strings with the same value refer to the same object. I also know that a string can be interned at runtime: string now =…
Motti
  • 110,860
  • 49
  • 189
  • 262
30
votes
7 answers

Best way to convert string to array of object in javascript?

I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea?
Zeck
  • 6,433
  • 21
  • 71
  • 111
29
votes
4 answers

How to use memset while handling strings in C++?

I am from Python background and recently learning C++. I was learning a C/C++ function called memset and following the online example from website https://www.geeksforgeeks.org/memset-in-cpp/ where I got some compilation errors: /** * @author …
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169
28
votes
3 answers

How to cast a String variable to a String Literal Type in Typescript

In Typescript, suppose I want to call a function with following signature- function foo(param: "TRUE"|"FALSE"|"NONE") How can I do something like- var str = runtimeString() if(str === "TRUE" | str === "FALSE" | str === "NONE") foo(str) Or, the…
Gulshan
  • 3,611
  • 5
  • 35
  • 46
28
votes
5 answers

Reference to string literals in Go

In my application I will frequently pass references to a static string. I wish to avoid having Go allocate memory for each call, but I failed to get the address to my string literal. Why is it not possible to take the address of a string literal…
ANisus
  • 74,460
  • 29
  • 162
  • 158
27
votes
4 answers

How to write unicode cross symbol in Java?

I'm trying to write this unicode cross symbol () in Java: class A { public static void main(String[] args) { System.out.println("\u2300"); System.out.println("\u10035"); } } I can write o with a line through it (⌀) just…
Dog
  • 7,707
  • 8
  • 40
  • 74
26
votes
4 answers

Is storage for the same content string literals guaranteed to be the same?

Is the code below safe? It might be tempting to write code akin to this: #include const std::map m = { {"text1", 1}, {"text2", 2} }; int main () { volatile const auto a = m.at("text1"); return 0; } The map…
luk32
  • 15,812
  • 38
  • 62
26
votes
4 answers

Invalid conversion from ‘const char*’ to ‘unsigned char*’

A simple C++ code: int main(){ unsigned char* t="123"; } on compilation with g++ gives following error: invalid conversion from ‘const char*’ to ‘unsigned char*’ [-fpermissive] Why?
anupamD
  • 862
  • 1
  • 8
  • 21
26
votes
2 answers

Using "constexpr" to use string literal for template parameter

I have written some code to cast const char* to int by using constexpr and thus I can use a const char* as a template argument. Here is the code: #include class conststr { public: template constexpr…
25
votes
3 answers

StringFormat is ignored

This is my binding (shortened, Command-Property is also bound) The Tag-Property of…
Reini
  • 1,233
  • 3
  • 19
  • 34
25
votes
4 answers

Difference between N'String' vs U'String' literals in Oracle

What is the meaning and difference between these queries? SELECT U'String' FROM dual; and SELECT N'String' FROM dual;
Ganesh
  • 486
  • 3
  • 8
  • 18