Lably
Skip to content

Constant (computer programming)

Value that is not changed during execution

  • 14 min read
  • 7 chapters

Learn it on Lably

Constants are useful for both programmers and compilers: for programmers, they are a form of self-documenting code and allow reasoning about correctness, while for compilers, they allow compile-time and run-time checks that verify that constancy assumptions are not violated, and allow or simplify some compiler optimizations.

There are various specific realizations of the general notion of a constant, with subtle distinctions that are often overlooked. The most significant are: compile-time (statically valued) constants, run-time (dynamically valued) constants, immutable objects, and constant types (const).

Typical examples of compile-time constants include mathematical constants, values from standards (here maximum transmission unit), or internal configuration values (here characters per line), such as these C examples:

<span id="mwJA"></span><span class="k" id="mwJQ">const</span><span class="w" id="mwJg"> </span><span class="kt" id="mwJw">float</span><span class="w" id="mwKA"> </span><span class="n" id="mwKQ">PI</span><span class="w" id="mwKg"> </span><span class="o" id="mwKw">=</span><span class="w" id="mwLA"> </span><span class="mf" id="mwLQ">3.1415927</span><span class="p" id="mwLg">;</span><span class="w" id="mwLw"> </span><span class="c1" id="mwMA">// maximal single float precision</span> <span class="k" id="mwMQ">const</span><span class="w" id="mwMg"> </span><span class="kt" id="mwMw">unsigned</span><span class="w" id="mwNA"> </span><span class="kt" id="mwNQ">int</span><span class="w" id="mwNg"> </span><span class="n" id="mwNw">MTU</span><span class="w" id="mwOA"> </span><span class="o" id="mwOQ">=</span><span class="w" id="mwOg"> </span><span class="mi" id="mwOw">1500</span><span class="p" id="mwPA">;</span><span class="w" id="mwPQ"> </span><span class="c1" id="mwPg">// Ethernet v2, RFC 894</span> <span class="k" id="mwPw">const</span><span class="w" id="mwQA"> </span><span class="kt" id="mwQQ">unsigned</span><span class="w" id="mwQg"> </span><span class="kt" id="mwQw">int</span><span class="w" id="mwRA"> </span><span class="n" id="mwRQ">COLUMNS</span><span class="w" id="mwRg"> </span><span class="o" id="mwRw">=</span><span class="w" id="mwSA"> </span><span class="mi" id="mwSQ">80</span><span class="p" id="mwSg">;</span>

Typical examples of run-time constants are values calculated based on inputs to a function, such as this C++ example:

<span id="mwTw"></span><span class="k" id="mwUA">import</span><span class="w" id="mwUQ"> </span><span class="n" id="mwUg">std</span><span class="p" id="mwUw">;</span> <span class="k" id="mwVA">using</span><span class="w" id="mwVQ"> </span><span class="n" id="mwVg">std</span><span class="o" id="mwVw">::</span><span class="n" id="mwWA">string</span><span class="p" id="mwWQ">;</span> <span class="kt" id="mwWg">void</span><span class="w" id="mwWw"> </span><span class="nf" id="mwXA">f</span><span class="p" id="mwXQ">(</span><span class="n" id="mwXg">string</span><span class="o" id="mwXw">&</span><span class="w" id="mwYA"> </span><span class="n" id="mwYQ">s</span><span class="p" id="mwYg">)</span><span class="w" id="mwYw"> </span><span class="p" id="mwZA">{</span> <span class="w" id="mwZQ"> </span><span class="k" id="mwZg">const</span><span class="w" id="mwZw"> </span><span class="kt" id="mwaA">size_t</span><span class="w" id="mwaQ"> </span><span class="n" id="mwag">len</span><span class="w" id="mwaw"> </span><span class="o" id="mwbA">=</span><span class="w" id="mwbQ"> </span><span class="n" id="mwbg">s</span><span class="p" id="mwbw">.</span><span class="n" id="mwcA">length</span><span class="p" id="mwcQ">();</span> <span class="w" id="mwcg"> </span><span class="c1" id="mwcw">// ...</span> <span class="p" id="mwdA">}</span>

Use

Some programming languages make an explicit syntactic distinction between constant and variable symbols, for example considering assignment to a constant to be a syntax error, while in other languages they are considered syntactically the same (both simply an identifier), and the difference in treatment is semantic (assignment to an identifier is syntactically valid, but if the identifier is a constant it is semantically invalid).

A constant value is defined once and can be referenced many times throughout a program. Using a constant instead of specifying the same value multiple times can simplify code maintenance (as in don't repeat yourself) and can be self documenting by supplying a meaningful name for a value, for instance, PI instead of 3.1415926.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Computing and communication