{"id":7254,"date":"2025-11-29T00:01:18","date_gmt":"2025-11-29T08:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7254"},"modified":"2025-11-22T12:55:46","modified_gmt":"2025-11-22T20:55:46","slug":"consistently-constant","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7254","title":{"rendered":"Consistently Constant"},"content":{"rendered":"<p>A new keyword added with the C23 standard is <em>constexpr<\/em>. It&#8217;s a storage class specifier that sets a constant value. Unlike the original C language qualifier, <em>const<\/em>, storage declared with the <em>constexpr<\/em> is truly constant and cannot be altered, as was demonstrated in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7248\">last week&#8217;s Lesson<\/a>.<br \/>\n<!--more--><br \/>\nThough the C23 standard is dated for the year 2023, not every compiler is current with it; implementation is happening slowly. Even then, the <code>-std=c23<\/code> switch (or <code>-std=c2x<\/code>) may be required to activate various C23 features, including recognition of the <em>constexpr<\/em> keyword.<\/p>\n<p>In a terminal window, use the command <code>clang --version<\/code> or <code>gcc --version<\/code> to obtain the compiler&#8217;s version. On my main computer, <em>clang<\/em> is version 14.0.0. I&#8217;ve installed later versions for testing, but nothing that fully supports C23 on this system.<\/p>\n<p>On my Linux test computer, <em>clang<\/em> is version 18.1.3, which doesn&#8217;t recognize the <em>constexpr<\/em> keyword even with the <code>-std=c23<\/code> switch applied. But I have installed <em>clang<\/em> version 19 (19.1.1), which does recognize <em>constexpr<\/em> but (again) only with the <code>-std=c23<\/code> switch used.<\/p>\n<p>Here is sample code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_29-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_11_29-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    constexpr int size = 16;\r\n\r\n    printf(\"The value is %d\\n\",size);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>As with the <em>const<\/em> qualifier, you must assign a value immediately when using <em>constexpr<\/em> to declare storage. In this code, <em>int<\/em> variable <code>size<\/code> is declared a constant expression and assigned a value of 16. Here&#8217;s the output:<\/p>\n<p><code>Size is 16<\/code><\/p>\n<p>To prove how bulletproof the <em>constexpr<\/em> is, I tried the same trick from last week&#8217;s Lesson to reset its value. Here&#8217;s an update to the code presented:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_29-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_11_29-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    constexpr int size = 16;\r\n    int *s;\r\n\r\n    printf(\"The value is %d\\n\",size);\r\n    s = (int *)&amp;size;\r\n    *s = 0;\r\n    printf(\"The value is %d\\n\",size);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code builds with no warnings or errors. It runs when compiled with C23 compatibility as described earlier. Here&#8217;s the output:<\/p>\n<p><code>Size is 16<br \/>\nSize is 16<\/code><\/p>\n<p>No errors are generated on output, but the constant <code>size<\/code> is unaltered. I suppose this result confirms the constant&#8217;s consistency on a superficial level; at least the storage seems more protected than shown in last week&#8217;s Lesson.<\/p>\n<p>I&#8217;ve read other interesting about how <em>constexpr<\/em> can be used, along with some limitations and proper procedures. I&#8217;ll most likely cover these details in the future when the C23 standard is more widely implemented. It&#8217;s been two years since I last <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6151\">wrote about C23<\/a>. Things are moving slowly, but at least they&#8217;re moving.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The only true constant declaration in C is found in the C23 standard and the <em>constexpr<\/em> qualifier. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7254\">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-7254","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\/7254","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=7254"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7254\/revisions"}],"predecessor-version":[{"id":7282,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7254\/revisions\/7282"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}