{"id":7248,"date":"2025-11-22T00:01:07","date_gmt":"2025-11-22T08:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7248"},"modified":"2025-11-29T11:46:26","modified_gmt":"2025-11-29T19:46:26","slug":"constantly-complaining","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7248","title":{"rendered":"Constantly Complaining"},"content":{"rendered":"<p>The C language has an issue with constants. As far as I can tell, three different ways are at your disposal to express a constant: constant expressions, literal constants, and constant types. More variety may be available, which adds to the confusion.<br \/>\n<!--more--><br \/>\n<strong>Constant Expressions.<\/strong> Also (and inconsistently) called <em>symbolic constants<\/em>, these are created by using the precompiler <code>#define<\/code> directive. For example:<\/p>\n<p><code>#define COUNT 30<\/code><\/p>\n<p>Traditionally, these constants are created in all caps, as shown with <code>COUNT<\/code> above. What the precompiler does is to replace all instances of <code>COUNT<\/code> in the source code file with the value or expression specified. This approach truly makes the expression constant, in that once the word is transformed into its assigned value the program cannot alter the value (or expression).<\/p>\n<p>I prefer use <code>#define<\/code> to set constants in my code. Though it&#8217;s not a requirement to make it all caps, doing so makes it easier for me to locate them. Writing these constants in all caps is also the tradition for symbolic constants defined in the various header files, such as <code>NULL<\/code>, <code>FILE<\/code>, and so on.<\/p>\n<p>In a way, the constant expression is the true constant qualifier in C &mdash; and it&#8217;s not even part of the language.<\/p>\n<p><strong>Literal Constants.<\/strong> A literal constant is any value you set directly into the code. Literals cannot be changed. Hence, they&#8217;re constant. But they&#8217;re also one-shot deals: If you need to repeat a value in the code, you must type it again. If you need to edit the value, you must hunt down all instances and change the value consistently. Doing so is a pain.<\/p>\n<p>Another problem with literal constants is that they occasionally become &#8220;<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4974\">magic numbers<\/a>,&#8221; which lack an explanation or reference as to their underlying purpose. (Click the link for an example.)<\/p>\n<p>Suffixes can be applied to some literal constants to confirm their values. These characters include:<\/p>\n<ul>\n<li><code>L<\/code> or <code>l<\/code> for a <em>long<\/em> integer<\/li>\n<li><code>U<\/code> or <code>u<\/code> for an <em>unsigned<\/em> integer<\/li>\n<li><code>UL<\/code> or <code>ul<\/code> for a <em>long unsigned<\/em> integer<\/li>\n<\/ul>\n<p>The <code>L<\/code> suffix can also be used as a prefix to specify a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2593\">wide string<\/a>: <code>L\"Wide string\"<\/code> Other prefixes include <code>0<\/code> (zero) for octal values and <code>0x<\/code> for hexadecimal.<\/p>\n<p>Character constants used in C include <code>\\n<\/code> and <code>\\0<\/code> among many others. These  represent values, not an assignable constant you can create.<\/p>\n<p><strong>Constant Types.<\/strong> A constant type is declared with the <em>const<\/em> qualifier. A data type and identifier (variable name) must be specified, along with an assigned value:<\/p>\n<p><code>const int count = 30;<\/code><\/p>\n<p>While this value cannot be altered in the code, it&#8217;s not technically a constant. The compiler flags any attempt to alter the value of <em>count<\/em> as a warning or error. According to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7182#comment-820\">a comment<\/a> made by subscriber M.Stumpfl, the <em>const<\/em> qualifier should really be a &#8220;read only&#8221; qualifier. Effectively, the read-only condition is what the compiler applies to the value.<\/p>\n<p>Various other online sources concur with sentiments about the <em>const<\/em> qualifier. In fact, you can write code that bypasses <em>const<\/em>, though I don&#8217;t recommend using it as the results are unpredictable:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_22-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_11_22-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int count = 30;\r\n    int *c;\r\n\r\n    printf(\"The count is %d\\n\",count);\r\n    c = (int *)&amp;count;\r\n    *c = 0;\r\n    printf(\"The count is %d\\n\",count);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code creates the constant <code>count<\/code>, assigned the value 30. The value is output, but then pointer <code>c<\/code> steals the address of variable <code>count<\/code>. Through the pointer, a new value is assigned to &#8220;constant&#8221; <code>count<\/code>: zero.<\/p>\n<p>The program builds without warnings or errors. Here is the output when compiled with <em>gcc<\/em>:<\/p>\n<p><code>The count is 30<br \/>\nThe count is 0<\/code><\/p>\n<p>Not so constant, eh?<\/p>\n<p>Building with <em>clang<\/em> works, though the program doesn&#8217;t alter the value. This inconsistency is enough proof that the technique isn&#8217;t reliable. But that&#8217;s not even the point!<\/p>\n<p>A better solution is offered in the C23 standard, the <em>constexpr<\/em> qualifier. I cover this new keyword in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7254\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does the C language really have constants? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7248\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7248","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7248"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7248\/revisions"}],"predecessor-version":[{"id":7283,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7248\/revisions\/7283"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}