Questions tagged [literals]

a notation for representing fixed values in source code

External links

1554 questions
31
votes
6 answers

Backslashes in single quoted strings vs. double quoted strings

If I add a backslash+space to the start of double and single quoted strings, I get different results: "\ text" '\ text' In the output for the double quoted string I see only a space. In the output for the single quoted string I see…
Tucker
  • 323
  • 1
  • 3
  • 5
31
votes
4 answers

Assigning 128-bit integer in C

When I try to assign an 128-bit integer in gcc 4.9.1, I get a warning: integer constant is too large for its type. Example Code int main(void) { __uint128_t p = 47942806932686753431; return 0; } Output I'm compiling with gcc -std=c11 -o test…
iblue
  • 29,609
  • 19
  • 89
  • 128
31
votes
8 answers

Should I use _T or _TEXT on C++ string literals?

For example: // This will become either SomeMethodA or SomeMethodW, // depending on whether _UNICODE is defined. SomeMethod( _T( "My String Literal" ) ); // Becomes either AnotherMethodA or AnotherMethodW. AnotherMethod( _TEXT( "My Text" ) ); I've…
Joe
  • 16,328
  • 12
  • 61
  • 75
30
votes
4 answers

Literal hashes in c#?

I've been doing c# for a long time, and have never come across an easy way to just new up a hash. I've recently become acquainted with the ruby syntax of hashes and wonder, does anyone know of a simple way to declare a hash as a literal, without…
DevelopingChris
  • 39,797
  • 30
  • 87
  • 118
30
votes
8 answers

Are string literals const?

Both GCC and Clang do not complain if I assign a string literal to a char*, even when using lots of pedantic options (-Wall -W -pedantic -std=c99): char *foo = "bar"; while they (of course) do complain if I assign a const char* to a char*. Does…
peoro
  • 25,562
  • 20
  • 98
  • 150
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
30
votes
5 answers

Best practice for long string literals in Go

I've got a long string literal in Go: db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')") I see two ways to make this more manageable: raw…
joshlf
  • 21,822
  • 11
  • 69
  • 96
30
votes
2 answers

How do I write a map literal in C++11?

In Python, I can write a map literal like this: mymap = {"one" : 1, "two" : 2, "three" : 3} How can I do the equivalent in C++11?
abw333
  • 5,571
  • 12
  • 41
  • 48
29
votes
2 answers

Getting the literal out of a python Literal type, at runtime?

How can I get the literal value out of a Literal[] from typing? from typing import Literal, Union Add = Literal['add'] Multiply = Literal['mul'] Action = Union[Add,Multiply] def do(a: Action): if a == Add: print("Adding!") elif a…
LudvigH
  • 3,662
  • 5
  • 31
  • 49
29
votes
10 answers

Is there a best practice for writing maps literal style in Java?

In short, if you want to write a map of e.g. constants in Java, which in e.g. Python and Javascript you would write as a literal, T CONSTANTS = { "CONSTANT_NAME_0": CONSTANT_VALUE_0 , "CONSTANT_NAME_1": CONSTANT_VALUE_1 , …
FK82
  • 4,907
  • 4
  • 29
  • 42
27
votes
1 answer

Why doesn't "0xe+1" compile?

Look at this code snippet: int a = 0xe+1; Clang, gcc, icc don't compile this: t.cpp:1:12: error: invalid suffix '+' on integer constant MSVC successfully compiles. Which compiler is correct? If clang and gcc are correct, why is this…
geza
  • 28,403
  • 6
  • 61
  • 135
27
votes
3 answers

Python's Passing by References

Hello I am trying to understand how Python's pass by reference works. I have an example: >>>a = 1 >>>b = 1 >>>id(a);id(b) 140522779858088 140522779858088 This makes perfect sense since a and b are both referencing the same value that they would…
Pulse
  • 527
  • 4
  • 11
26
votes
1 answer

What is the benefit of std::literals::.. being inline namespaces?

In the C++-Standard (eg. N4594) there are two definitions for operator""s: One for std::chrono::seconds : namespace std { ... inline namespace literals { inline namespace chrono_literals { // 20.15.5.8, suffixes for duration literals constexpr…
towi
  • 21,587
  • 28
  • 106
  • 187
26
votes
3 answers

C# suffix behind numeric literal

I am new to C# and want to understand how values work. If I look at a normal integer value, it has 3 important parts in it: the type, name and value. int testInt = 3; | | | Type Name Value But when I see a float value it confuses me a…
user3772108
  • 854
  • 2
  • 14
  • 32
26
votes
2 answers

Why is 0.5==0.5f is true but 0.1==0.1f is false?

Please look at following two code: public static void main(String... args) { System.out.println(0.5==0.5f); } Output : true public static void main(String... args) { System.out.println(0.1==0.1f); } Output: false Why is it happening so?
Anurag Shukla
  • 379
  • 1
  • 3
  • 11