{"id":6151,"date":"2023-12-16T00:01:07","date_gmt":"2023-12-16T08:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6151"},"modified":"2023-12-23T08:21:14","modified_gmt":"2023-12-23T16:21:14","slug":"welcome-c23","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6151","title":{"rendered":"Welcome C23"},"content":{"rendered":"<p>The next C standard is established and it&#8217;s slowly being implemented. If you have the latest version of the C compiler installed on your system, you can take advantage of some of the fun and improved features of the C programming language.<br \/>\n<!--more--><br \/>\nI plan on covering some C23 features over the next few Lessons. Some of the features are available when using the current C standard (C17), providing you employ a few workarounds.<\/p>\n<p>For example, C23 adds the reserved words <em>bool<\/em>, <em>true<\/em>, and <em>false<\/em>.<\/p>\n<p>The <em>bool<\/em> keyword identifies a Boolean data type, which is a value of either one and zero.<\/p>\n<p>The <em>true<\/em> and <em>false<\/em> keywords identify TRUE and FALSE values, where TRUE is any non-zero value and FALSE is zero. Yes, these are technically <em>bool<\/em> constants.<\/p>\n<p>These new keywords are a great addition to C, and something that other programming languages have naturally. As keywords, <em>bool<\/em>, <em>true<\/em>, and <em>false <\/em>may cause some issues where older C code may use them as identifiers. Still, I still believe them to be a great and necessary addition to the language.<\/p>\n<p>If you want to use these words in your code now, all just add <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2956\">the <code>stdbool.h<\/code> header<\/a> to your code, as shown in the following example.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_16-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_12_16-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nint main()\r\n{\r\n    bool b = 1;\r\n\r\n    if( b==true )\r\n        puts(\"B is true\");\r\n    else\r\n        puts(\"B is false\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses the <em>bool<\/em> and <em>true<\/em> keywords, though they&#8217;re defined in the <code>stdbool.h<\/code> header file. As such, this code compiles under the current version of C. Here is the output:<\/p>\n<p><code>B is true<\/code><\/p>\n<p>If you remove the <code>#include<\/code> directive (or comment it out), the compiler generates some errors:<\/p>\n<p><span style=\"font-family:Courier; font-weight: bold; font-size:12px; color:red; line-height:12px\">1216b.c:5:2: error: use of undeclared identifier &#8216;bool&#8217;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool b = 1;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<br \/>\n1216b.c:7:6: error: use of undeclared identifier &#8216;b&#8217;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( b==true )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<br \/>\n1216b.c:7:9: error: use of undeclared identifier &#8216;true&#8217;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( b==true )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<br \/>\n3 errors generated.<\/span><\/p>\n<p>But if you compile with C23 features activated, the code builds.<\/p>\n<p>To get those C23 features, or at least some of them, ensure that you have the latest C compiler installed. On my system, I obtained the <em>clang-15<\/em> update, which is the most recent stable release available for my distro. According to the <a href=\"https:\/\/clang.llvm.org\/c_status.html#c2x\" rel=\"noopener\" target=\"_blank\">LLVM website<\/a>, we may have to wait until <em>clang-18<\/em> until most of the C23 features are implemented. Still, enough of these new C language features are available that you can try them out, as I did with the following code and <em>clang-15<\/em>:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_16-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_12_16-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    bool b = 1;\r\n\r\n    if( b==true )\r\n        puts(\"B is true\");\r\n    else\r\n        puts(\"B is false\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Yes, this is the same program as before, minus including the <code>stdbool.h<\/code> header. I used the following command to build the program:<\/p>\n<p><code>clang-15 -Wall -std=c2x 2023_12_16-Lesson-b.c<\/code><\/p>\n<p>I need to type <strong>clang-15<\/strong> as the system version of <em>clang<\/em> is mapped to <em>clang-14<\/em>. (I could change it, but meh.) The <strong>-Wall<\/strong> switch activates all warnings. The <strong>-std=c2x<\/strong> switch directs the compiler to use the C23 standard, as much of it as is implemented.<\/p>\n<p>The program builds with no warnings or errors, which tells me that for <em>clang-15<\/em> and when using the <code>-std=c2x<\/code> switch, I can work with some C23 features. Here&#8217;s the output:<\/p>\n<p><code>B is true<\/code><\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6152\">next week&#8217;s Lesson<\/a>, I describe how to write code that identifies the C23 compiler version and to implement code that takes advantage of some of its features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s the first major update to the C programming language in a long time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6151\">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-6151","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\/6151","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=6151"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6151\/revisions"}],"predecessor-version":[{"id":6187,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6151\/revisions\/6187"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}