{"id":6152,"date":"2023-12-23T00:01:44","date_gmt":"2023-12-23T08:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6152"},"modified":"2023-12-30T10:04:09","modified_gmt":"2023-12-30T18:04:09","slug":"testing-for-c23-compatibility","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6152","title":{"rendered":"Testing for C23 Compatibility"},"content":{"rendered":"<p>The C23 standard is only partially implemented on a few compilers. To ensure that you can build C23 programs, you must obtain the latest compiler version from your distro&#8217;s package manager. Even then, not all the C23 capabilities may be available.<br \/>\n<!--more--><br \/>\nTwo things are required to test for C23 compatibility.<\/p>\n<p>First, the compiler standard switch, also called the <em>flavor<\/em>, must be set to C23. This switch is <code>&#8209;std=<em>nnn<\/em><\/code> where <em>nnn<\/em> is the C standard to use for compiling.<\/p>\n<p>All C compilers have a default configuration. For most of today&#8217;s compilers, it&#8217;s the C17 standard. To use another standard, such as C99, you must compile with the switch <code>&#8209;std=c99<\/code>. For C23, here is the command line format:<\/p>\n<p><code>clang -std=c99 source.c<\/code><\/p>\n<p>I&#8217;m using <em>clang<\/em> as my compiler above, though you can use <em>gcc<\/em> or whichever compiler you prefer; the switch is identical. The file <code>source.c<\/code> is built adhering to only the C99 standard. A programmer may want to assert this standard for compatibility with an older system.<\/p>\n<p>The <code>-std=c2x<\/code> switch implements the C23 standard. Don&#8217;t use <code>-std=c23<\/code> (<code>c23<\/code> instead of <code>c2x<\/code>), which isn&#8217;t recognized &mdash; yet.<\/p>\n<p>Second, you must test the C version number held in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5664\">the <code>__STDC_VERSION__<\/code> defined constant<\/a>. See those double underscores? They&#8217;re used internally to flag special defined constants.<\/p>\n<p>The following code outputs the <code>__STDC_VERSION__<\/code> value and tests for C23 compatibility.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_23-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_12_23-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"C version %ld\\n\",__STDC_VERSION__);\r\n    if( __STDC_VERSION__ &gt;= 202000 )\r\n        puts(\"C23 features available\");\r\n    else\r\n        puts(\"C23 features unavailable\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>printf()<\/em> statement outputs the value of the <code>__STDC_VERSION__<\/code> defined constant. The <em>if<\/em> test compares this value with 202000, which seems to be the current value for C23 implementation across various compilers.<\/p>\n<p>Here&#8217;s a sample run when compiled under <em>clang<\/em> without the <code>-std=c2x<\/code> switch:<\/p>\n<p><code>C version 201710<br \/>\nC23 features unavailable<\/code><\/p>\n<p>If I use the <code>-std=c2x<\/code> switch on the regular <em>clang<\/em> compiler, I get this output:<\/p>\n<p><code>C version 202000<br \/>\nC23 features available<\/code><\/p>\n<p>I find this output questionable, as this same configuration is unable to build the sample code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6151\">last week&#8217;s Lesson<\/a>.<\/p>\n<p>Here is the output under <em>clang-15<\/em> without the <code>-std=c2x<\/code> switch:<\/p>\n<p><code>C version 201710<br \/>\nC23 features unavailable<\/code><\/p>\n<p>Here is the output under <em>clang-15<\/em> with the <code>-std=c2x<\/code> switch:<\/p>\n<p><code>C version 201710<br \/>\nC23 features available<\/code><\/p>\n<p>In this configuration, the C23 test program from last week&#8217;s Lesson builds with no errors.<\/p>\n<p>For the purposes of moving forward, ensure that you not only have a C compiler that not only accepts the <code>-std=c2x<\/code> switch, but can build C23 code. Apparently the value of <code>__STDC_VERSION__<\/code> isn&#8217;t reliable. I&#8217;m unaware of any other tests, other than just running C23 code to see whether it builds without any errors.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6173\">next week&#8217;s Lesson<\/a>, I cover the way C23 deals with binary integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C23 standard is only partially implemented on a few compilers. To ensure that you can build C23 programs, you must obtain the latest compiler version from your distro&#8217;s package manager. Even then, not all the C23 capabilities may be &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6152\">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-6152","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\/6152","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=6152"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6152\/revisions"}],"predecessor-version":[{"id":6201,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6152\/revisions\/6201"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}