Questions tagged [constants]
Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant. A const-qualified data storage area (object, field, variable, parameter) is one that "never changes", thus allowing extra code generator optimizations and additional static checking of program correctness.
constants
10,339
questions
1804
votes
23
answers
729k
views
What is the difference between const int*, const int * const, and int const *?
I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do?
I want to know all the do's and all don'ts in terms ...
1697
votes
30
answers
514k
views
What is the difference between const and readonly in C#?
What is the difference between const and readonly in C#?
When would you use one over the other?
1593
votes
22
answers
482k
views
'Static readonly' vs. 'const'
I've read around about const and static readonly fields. We have some classes which contain only constant values. They are used for various things around in our system. So I am wondering if my ...
1386
votes
44
answers
1.4m
views
How do I create a constant in Python?
How do I declare a constant in Python?
In Java, we do:
public static final String CONST_NAME = "Name";
1179
votes
33
answers
517k
views
Are there constants in JavaScript?
Is there a way to use constants in JavaScript?
If not, what's the common practice for specifying variables that are used as constants?
1048
votes
14
answers
447k
views
Constants in Objective-C
I'm developing a Cocoa application, and I'm using constant NSStrings as ways to store key names for my preferences.
I understand this is a good idea because it allows easy changing of keys if ...
861
votes
10
answers
356k
views
What's the difference between constexpr and const?
What's the difference between constexpr and const?
When can I use only one of them?
When can I use both and how should I choose one?
772
votes
9
answers
267k
views
PHP | define() vs. const
In PHP, you can declare constants in two ways:
With define keyword
define('FOO', 1);
Using const keyword
const FOO = 1;
What are the main differences between those two?
When and why should you use ...
771
votes
6
answers
711k
views
What is meant with "const" at end of function declaration? [duplicate]
I got a book, where there is written something like:
class Foo
{
public:
int Bar(int random_arg) const
{
// code
}
};
What does it mean?
693
votes
8
answers
371k
views
Why Is `Export Default Const` invalid?
I see that the following is fine:
const Tab = connect( mapState, mapDispatch )( Tabs );
export default Tab;
However, this is incorrect:
export default const Tab = connect( mapState, mapDispatch )( ...
671
votes
16
answers
625k
views
Declare a const array
Is it possible to write something similar to the following?
public const string[] Titles = { "German", "Spanish", "Corrects", "Wrongs" };
669
votes
17
answers
389k
views
"static const" vs "#define" vs "enum"
Which one is better to use among the below statements in C?
static const int var = 5;
or
#define var 5
or
enum { var = 5 };
582
votes
14
answers
395k
views
What is the difference between char s[] and char *s?
In C, one can use a string literal in a declaration like this:
char s[] = "hello";
or like this:
char *s = "hello";
So what is the difference? I want to know what actually happens in terms of ...
551
votes
9
answers
346k
views
C# naming convention for constants?
private const int THE_ANSWER = 42;
or
private const int theAnswer = 42;
Personally I think with modern IDEs we should go with camelCase as ALL_CAPS looks strange. What do you think?
529
votes
31
answers
396k
views
Does using const on function parameters have any effect? Why does it not affect the function signature?
For example, imagine a simple mutator that takes a single boolean parameter:
void SetValue(const bool b) { my_val_ = b; }
Does that const actually have any impact? Personally I opt to use it ...
529
votes
11
answers
494k
views
How can you define a static data member of type const std::string?
I'd like to have a private static constant for a class (in this case a shape-factory).
I'd like to have something of the sort.
class A {
private:
static const string RECTANGLE = "...
498
votes
17
answers
196k
views
What is the difference between the "const" and "final" keywords in Dart?
What is the difference between the const and final keywords in Dart?
471
votes
6
answers
220k
views
const vs constexpr on variables [duplicate]
Is there a difference between the following definitions?
const double PI = 3.141592653589793;
constexpr double PI = 3.141592653589793;
If not, which style is preferred in C++11?
465
votes
9
answers
114k
views
What is the difference between "const" and "val"?
I have recently read about the const keyword, and I'm so confused! I can't find any difference between const and the val keyword, I mean we can use both of them to make an immutable variable, is there ...
452
votes
17
answers
364k
views
PHP Constants Containing Arrays?
This failed:
define('DEFAULT_ROLES', array('guy', 'development team'));
Apparently, constants can't hold arrays. What is the best way to get around this?
define('DEFAULT_ROLES', 'guy|development ...
435
votes
6
answers
181k
views
Why can't I have "public static const string S = "stuff"; in my Class?
When trying to compile my class I get an error:
The constant 'NamespaceName.ClassName.CONST_NAME' cannot be marked static.
at the line:
public static const string CONST_NAME = "blah";
I could ...
418
votes
20
answers
406k
views
Declaring static constants in ES6 classes?
I want to implement constants in a class, because that's where it makes sense to locate them in the code.
So far, I have been implementing the following workaround with static methods:
class MyClass ...
394
votes
19
answers
300k
views
Why does JSHint throw a warning if I am using const?
This is the error I get when using const:
<error line="2" column="1" severity="warning" message="'const' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)...
389
votes
18
answers
241k
views
Const in JavaScript: when to use it and is it necessary?
I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js):
const x = ...
380
votes
15
answers
319k
views
Constants in Kotlin -- what's a recommended way to create them?
How is it recommended to create constants in Kotlin? And what's the naming convention? I've not found that in the documentation.
companion object {
//1
val MY_CONST = "something"
//2
...
370
votes
28
answers
892k
views
What is the best way to implement constants in Java? [closed]
I've seen examples like this:
public class MaxSeconds {
public static final int MAX_SECONDS = 25;
}
and supposed that I could have a Constants class to wrap constants in, declaring them static ...
369
votes
19
answers
262k
views
What is the difference between char * const and const char *?
What's the difference between:
char * const
and
const char *
368
votes
3
answers
63k
views
"sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers" warning
I have Constants NSString, that I want to call like:
[newString isEqualToString:CONSTANT_STRING];
Any wrong code here?
I got this warning:
sending 'const NSString *' to parameter of type '...
359
votes
8
answers
93k
views
Are variables declared with let or const hoisted?
I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected...
console.log(typeof name); // undefined
var name = "John";
...variables ...
356
votes
6
answers
366k
views
Proper use of const for defining functions
Are there any limits to what types of values can be set using const in JavaScript, and in particular, functions? Is this valid? Granted it does work, but is it considered bad practice for any reason?
...
314
votes
2
answers
22k
views
What does the constant 0.0039215689 represent?
I keep seeing this constant pop up in various graphics header files
0.0039215689
It seems to have something to do with color maybe?
Here is the first hit on Google:
void RDP_G_SETFOGCOLOR(void)
{
...
272
votes
9
answers
466k
views
Difference between char* and const char*?
What's the difference between
char* name
which points to a constant string literal, and
const char* name
270
votes
8
answers
435k
views
Constant pointer vs Pointer to constant [duplicate]
I want to know the difference between
const int* ptr;
and
int * const ptr;
and how it works.
It is pretty difficult for me to understand or keep remember this.
Please help.
270
votes
5
answers
300k
views
Declare a constant array
I have tried:
const ascii = "abcdefghijklmnopqrstuvwxyz"
const letter_goodness []float32 = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,....
266
votes
11
answers
197k
views
'static const' vs. '#define'
Is it better to use static const variables than #define preprocessor? Or does it maybe depend on the context?
What are advantages/disadvantages for each method?
257
votes
11
answers
112k
views
How come a non-const reference cannot bind to a temporary object?
Why is it not allowed to get non-const reference to a temporary object,
which function getx() returns? Clearly, this is prohibited by C++ Standard
but I am interested in the purpose of such ...
255
votes
6
answers
18k
views
Why is 0 < -0x80000000?
I have below a simple program:
#include <stdio.h>
#define INT32_MIN (-0x80000000)
int main(void)
{
long long bal = 0;
if(bal < INT32_MIN )
{
printf("Failed!!!")...
245
votes
17
answers
249k
views
How to select multiple rows filled with constants?
Selecting constants without referring to a table is perfectly legal in an SQL statement:
SELECT 1, 2, 3
The result set that the latter returns is a single row containing the values. I was wondering ...
240
votes
11
answers
126k
views
Is it better in C++ to pass by value or pass by reference-to-const?
Is it better in C++ to pass by value or pass by reference-to-const?
I am wondering which is better practice. I realize that pass by reference-to-const should provide for better performance in the ...
237
votes
13
answers
165k
views
Ruby on Rails: Where to define global constants?
I'm just getting started with my first Ruby on Rails webapp. I've got a bunch of different models, views, controllers, and so on.
I'm wanting to find a good place to stick definitions of truly ...
214
votes
13
answers
165k
views
Why can I change a constant object in JavaScript?
I know that ES6 is not standardized yet, but a lot of browsers currently support const keyword in JS.
In spec, it is written that:
The value of a constant cannot change through re-assignment, and a
...
212
votes
5
answers
97k
views
How can I get all constants of a type by reflection?
How can I get all constants of any type using reflection?
211
votes
4
answers
26k
views
Why isn't String.Empty a constant?
In .NET why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.
205
votes
15
answers
98k
views
Include constant in string without concatenating
Is there a way in PHP to include a constant in a string without concatenating?
define('MY_CONSTANT', 42);
echo "This is my constant: MY_CONSTANT";
205
votes
3
answers
129k
views
Boolean literals in PowerShell
What are the Boolean literals in PowerShell?
201
votes
3
answers
49k
views
What is the temporal dead zone?
I've heard that accessing let and const values before they are initialized can cause a ReferenceError because of something called the temporal dead zone.
What is the temporal dead zone, how does it ...
200
votes
3
answers
135k
views
Go naming conventions for const [closed]
I'm trying to determine whether there is a naming convention for the names of const in Golang.
I personally would tend to follow the C style and write them in upper case, but I haven't found anything ...
199
votes
7
answers
110k
views
C++: const reference, before vs after type-specifier
What is the difference between the arguments in:
int foo1(const Fred &arg) {
...
}
and
int foo2(Fred const &arg) {
...
}
?
I don't see this case covered in the parashift FAQ.
199
votes
4
answers
292k
views
How to sort with a lambda?
sort(mMyClassVector.begin(), mMyClassVector.end(),
[](const MyClass & a, const MyClass & b)
{
return a.mProperty > b.mProperty;
});
I'd like to use a lambda function to sort ...
198
votes
2
answers
807k
views
What does the PHP error message "Use of undefined constant" mean?
PHP is writing this error in the logs: "Notice: Use of undefined constant".
Error in logs:
PHP Notice: Use of undefined constant department - assumed 'department' (line 5)
PHP Notice: Use of ...