{"slug": "symbolic-constant-conundrum", "title": "Symbolic Constant Conundrum", "summary": "The article explains that symbolic constants, which replace literal values with named symbols, are used in programming to avoid \"magic numbers\" and improve code clarity. It details the evolution of methods for defining these constants in C and C++, including `#define` macros, `const`, `enum`, and `constexpr`, noting that `constexpr` (available in C++11 and C23) is the best option because it creates true constants, while `#define` should be used only as a last resort due to scope issues.", "body_md": "## Introduction\n\nA *symbolic constant* in any programming language is a name — a *symbol* — that can be used to stand in for a *constant* — a literal value. Programming languages inherited symbolic constants from mathematics that has many of them grouped by specific field of study. Examples include: π (pi), *c* (speed of light), e (Euler’s number), G (gravitational constant), *h* (Plank’s constant), etc. While those exact constants can be defined and used in programs, many programs define program-specific constants. Using constants is better than using [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)).\n\nBoth C and C++ have acquired multiple ways to specify symbolic constants as their respective languages have evolved over the decades, namely:\n\n- Macros (via\n`#define`\n\n). - Enumerations.\n-\n`const`\n\n. -\n`constexpr`\n\n.\n\nKnowing which of the ways to use in a particular case can be quite the conundrum.\n\n## Macros\n\nOriginally, C *only* had [macros](https://dev.to/pauljlucas/cc-preprocessor-macros-fh5), specifically, object-like macros, e.g.:\n\n```\n#define BUF_SIZE  8192\n```\n\nMacros are adequate, but not *good*. Why? Macros in general ignore scope, so you typically have to give macros *very* specific (long) names to avoid collision.\n\n## Enumerations\n\nEnumerations in [C](https://dev.to/pauljlucas/enumerations-in-c-ae7) and [C++](https://dev.to/pauljlucas/enumerations-in-c-pn9) are better, especially for declaring a set of *related* constants. In C++ with `enum class`\n\n, they can even be scoped to avoid collisions; in C, however, they’s still in the global scope.\n\nThe other caveat is that they can be constants only for integral values.\n\n## `const`\n\nAs I described for [C](https://dev.to/pauljlucas/c-const-conundrum-j2l) and [C++](https://dev.to/pauljlucas/const-conundrum-2bfj), you can use `const`\n\nfor constants, e.g.:\n\n``` js\nstatic unsigned const BUF_SIZE = 8192;\n\nchar BUF[ BUF_SIZE ];\n\nint main() {\n  char local_buf[ BUF_SIZE ];\n  // ...\n}\n```\n\nIn C++, that will compile just fine without warning; in C, it’ll either be accepted with warnings or rejected entirely, especially if you disable language extensions. Why? Because `const`\n\nis a misnomer since it really means *immutable*, not *constant*, and C is more picky about it.\n\nWhile the declaration of `BUF`\n\nmight be accepted, the declaration of `local_buf`\n\nwill either be considered a variable length array (VLA) (that, as I [pointed out](https://dev.to/pauljlucas/obscure-c99-array-features-3270), you should probably never use), or rejected since VLAs are an optional feature and not all compilers support them (notably, Microsoft’s C compiler doesn’t).\n\nA common work-around in C (prior to C23, see below) is to (ab)use `enum`\n\n:\n\n```\nenum {\n  BUF_SIZE = 8192\n};\n```\n\nThat is, use a nameless enumeration. The advantage is that enumeration constants really are *constant*.\n\n## `constexpr`\n\nIf you’re using C++11 or later, or C23 or later, there’s `constexpr`\n\n. Unlike `const`\n\n, `constexpr`\n\nreally means *constant*. This is by far the best option for declaring constants:\n\n```\nconstexpr unsigned BUF_SIZE = 8192;\n```\n\n## Conclusion\n\nTo summarize:\n\n- If you’re declaring a set of related, integral constants, use\n`enum`\n\n(in C) or`enum class`\n\n(in C++). - Otherwise, use\n`constexpr`\n\nif you can. - Otherwise, use\n`const`\n\n. - Otherwise, use\n`#define`\n\nas a last resort.", "url": "https://wpnews.pro/news/symbolic-constant-conundrum", "canonical_source": "https://dev.to/pauljlucas/symbolic-constant-conundrum-40e7", "published_at": "2026-05-23 00:34:36+00:00", "updated_at": "2026-05-23 01:02:59.549895+00:00", "lang": "en", "topics": ["developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/symbolic-constant-conundrum", "markdown": "https://wpnews.pro/news/symbolic-constant-conundrum.md", "text": "https://wpnews.pro/news/symbolic-constant-conundrum.txt", "jsonld": "https://wpnews.pro/news/symbolic-constant-conundrum.jsonld"}}