Questions tagged [literals]

a notation for representing fixed values in source code

External links

1554 questions
154
votes
6 answers

Why does long long n = 2000*2000*2000*2000; overflow?

long long int n = 2000*2000*2000*2000; // overflow long long int n = pow(2000,4); // works long long int n = 16000000000000; // works Why does the first one overflow (multiplying integer literal constants to assign to a long…
Fabbiucciello
  • 1,514
  • 2
  • 4
  • 9
151
votes
4 answers

Setting Short Value Java

I am writing a little code in J2ME. I have a class with a method setTableId(Short tableId). Now when I try to write setTableId(100) it gives compile time error. How can I set the short value without declaring another short variable? When setting…
Mubashar
  • 12,300
  • 11
  • 66
  • 95
146
votes
8 answers

Why write 1,000,000,000 as 1000*1000*1000 in C?

In code created by Apple, there is this line: CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 ) Is there any reason to express 1,000,000,000 as 1000*1000*1000? Why not 1000^3 for that matter?
Duck
  • 34,902
  • 47
  • 248
  • 470
144
votes
7 answers

How do I write a short literal in C++?

Very basic question: how do I write a short literal in C++? I know the following: 2 is an int 2U is an unsigned int 2L is a long 2LL is a long long 2.0f is a float 2.0 is a double '\2' is a char. But how would I write a short literal? I tried 2S…
Kip
  • 107,154
  • 87
  • 232
  • 265
118
votes
3 answers

Why does Python 3 allow "00" as a literal for 0 but not allow "01" as a literal for 1?

Why does Python 3 allow "00" as a literal for 0 but not allow "01" as a literal for 1? Is there a good reason? This inconsistency baffles me. (And we're talking about Python 3, which purposely broke backward compatibility in order to achieve goals…
walrus
  • 2,945
  • 5
  • 18
  • 19
113
votes
4 answers

escaping question mark in regex javascript

This is a simple question I think. I am trying to search for the occurrence of a string in another string using regex in JavaScript like so: var content ="Hi, I like your Apartment. Could we schedule a viewing? My phone number is: "; var gent =…
Andrew
  • 3,650
  • 9
  • 31
  • 32
113
votes
1 answer

What's with the integer cache maintained by the interpreter?

After dive into Python's source code, I find out that it maintains an array of PyInt_Objects ranging from int(-5) to int(256) (@src/Objects/intobject.c) A little experiment proves it: >>> a = 1 >>> b = 1 >>> a is b True >>> a = 257 >>> b = 257 >>> a…
felix021
  • 1,936
  • 3
  • 16
  • 20
109
votes
5 answers

Python regex - r prefix

Can anyone explain why example 1 below works, when the r prefix is not used? I thought the r prefix must be used whenever escape sequences are used. Example 2 and example 3 demonstrate this. # example 1 import re print (re.sub('\s+', ' ', 'hello …
JT.
  • 1,099
  • 2
  • 8
  • 3
108
votes
5 answers

Java equivalent of C#'s verbatim strings with @

Quick question. Is there an equivalent of @ as applied to strings in Java: For example I can do @"c:\afolder\afile" in C# and have it ignore the escape characters when processing instead of having to do "c:\\afolder\\aFile". Is there a Java…
Simon Rigby
  • 1,786
  • 4
  • 17
  • 28
100
votes
3 answers

How do you declare a Char literal in Visual Basic .NET?

With Option Strict On: Dim theLetterA As Char = "A" returns an error about converting the string "A" to a Char. What is the syntax to enter a Char literal?
Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
93
votes
4 answers

Why can I assign 0.0 to enumeration values, but not 1.0

Just out of curiosity: why can I assign 0.0 to a variable that is of an enumeration type, but not 1.0? Have a look at the following code: public enum Foo { Bar, Baz } class Program { static void Main() { Foo value1 = 0.0; …
feO2x
  • 5,358
  • 2
  • 37
  • 46
92
votes
13 answers

SQL Server - boolean literal?

How to write literal boolean value in SQL Server? See sample use: select * from SomeTable where PSEUDO_TRUE another sample: if PSEUDO_TRUE begin select 'Hello, SQL!' end Note: The query above has nothing to do with how I'm going to use it. It…
kazinix
  • 28,987
  • 33
  • 107
  • 157
86
votes
4 answers

What's the C++ suffix for long double literals?

In C++ (and C), a floating point literal without suffix defaults to double, while the suffix f implies a float. But what is the suffix to get a long double? Without knowing, I would define, say, const long double x =…
Walter
  • 44,150
  • 20
  • 113
  • 196
85
votes
3 answers

What are the Java semantics of an escaped number in a character literal, e.g. '\15' ?

Please explain what, exactly, happens when the following sections of code are executed: int a='\15'; System.out.println(a); this prints out 13; int a='\25'; System.out.println(a); this prints out 21; int a='\100'; System.out.println(a); this…
VAr
  • 2,551
  • 1
  • 27
  • 40
84
votes
12 answers

Is there a boolean literal in SQLite?

I know about the boolean column type, but is there a boolean literal in SQLite? In other languages, this might be true or false. Obviously, I can use 0 and 1, but I tend to avoid so-called "magic numbers" where possible. From this list, it seems…
Benjamin Oakes
  • 12,262
  • 12
  • 65
  • 83