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
Filter by
Sorted by
Tagged with
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 ...
user avatar
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?
readonly's user avatar
  • 349k
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 ...
Svish's user avatar
  • 155k
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";
zfranciscus's user avatar
  • 16.5k
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?
fuentesjr's user avatar
  • 51.6k
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 ...
Allyn's user avatar
  • 20.4k
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?
MBZ's user avatar
  • 27k
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 ...
danjarvis's user avatar
  • 10.1k
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?
aPoC's user avatar
  • 7,803
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 )( ...
Kayote's user avatar
  • 15.1k
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" };
Jaime Oro's user avatar
  • 10.1k
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 };
Vijay's user avatar
  • 66.4k
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 ...
user avatar
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?
mmiika's user avatar
  • 10.1k
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 ...
Rob's user avatar
  • 77.5k
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 = "...
lb.'s user avatar
  • 5,716
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?
Ishmal Ijaz's user avatar
  • 5,517
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?
fredoverflow's user avatar
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 ...
Mathew Hany's user avatar
  • 13.7k
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 ...
Nick Heiner's user avatar
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 ...
jjnguy's user avatar
  • 138k
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 ...
Jérôme Verstrynge's user avatar
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="&apos;const&apos; is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)...
Andre Schlesinger's user avatar
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 = ...
axdg's user avatar
  • 4,035
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 ...
Jodimoro's user avatar
  • 4,535
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 ...
mk.'s user avatar
  • 26.2k
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 *
LB.'s user avatar
  • 13.9k
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 '...
user4951's user avatar
  • 32.6k
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 ...
Luboš Turek's user avatar
  • 6,443
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? ...
David Sinclair's user avatar
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) { ...
crush's user avatar
  • 16.9k
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
Iceman's user avatar
  • 4,282
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.
Venkatesh K's user avatar
  • 4,434
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,....
ceth's user avatar
  • 44.8k
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?
Patrice Bernassola's user avatar
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 ...
Alexey Malistov's user avatar
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!!!")...
Jayesh Bhoi's user avatar
  • 25.3k
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 ...
Blagovest Buyukliev's user avatar
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 ...
Matt Pascoe's user avatar
  • 8,801
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 ...
AlexC's user avatar
  • 3,640
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 ...
Salvador Dali's user avatar
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?
masoud ramezani's user avatar
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.
travis's user avatar
  • 36.1k
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";
Brian's user avatar
  • 27.1k
205 votes
3 answers
129k views

Boolean literals in PowerShell

What are the Boolean literals in PowerShell?
Colonel Panic's user avatar
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 ...
joews's user avatar
  • 30.1k
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 ...
LtWorf's user avatar
  • 7,410
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.
eisbaw's user avatar
  • 2,748
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 ...
BTR's user avatar
  • 4,980
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 ...
Nik's user avatar
  • 2,434

1
2 3 4 5
207