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.

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.

The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what they can do.

In addition, a method can be declared as const. In this case, the this pointer inside such a method is of const ThisClass* const type rather than of ThisClass* const type. This means that non-const methods for this object cannot be called from inside such a method, nor can member variables be modified. In C++, a member variable can be declared as mutable, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and data synchronization. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

The C++11 standard adds additional notes on the meaning of const when used in the Standard Library with respect to thread access and possible modifications of those const objects.

More about const-correctness here.

10286 questions
23 answers

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 of assignments, passing to the functions, etc.
30 answers

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?
  • 343,444
  • 107
  • 203
  • 205
22 answers

'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 observation is correct: Should these kind of constant…
  • 152,914
  • 173
  • 462
  • 620
44 answers

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";
  • 16,175
  • 10
  • 47
  • 53
33 answers

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?
  • 50,920
  • 27
  • 77
  • 81
11 answers

How to convert a std::string to const char* or char*

How can I convert an std::string to a char* or a const char*?
  • 13,904
  • 9
  • 37
  • 43
14 answers

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 necessary. Plus, it's the whole 'separate your data from…
  • 20,271
  • 16
  • 57
  • 68
12 answers

What is the meaning of 'const' at the end of a member function declaration?

What is the meaning of const in declarations like these? class foobar { public: operator int () const; const char* foo() const; };
10 answers

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?
  • 26,084
  • 47
  • 114
  • 191
9 answers

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 one and when use the other?
  • 10,040
  • 3
  • 22
  • 21
6 answers

What is meant with "const" at end of function declaration?

I got a book, where there is written something like: class Foo { public: int Bar(int random_arg) const { // code } }; What does it mean?
  • 7,633
  • 3
  • 16
  • 8
17 answers

"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 };
  • 65,327
  • 90
  • 227
  • 319
7 answers

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 )( Tabs ); Yet this is fine: export default Tab =…
  • 14,579
  • 25
  • 85
  • 144
16 answers

Declare a const array

Is it possible to write something similar to the following? public const string[] Titles = { "German", "Spanish", "Corrects", "Wrongs" };
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39
14 answers

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 storage duration, both at compile and run time.
2 3
99 100