{"id":5664,"date":"2022-12-10T00:01:03","date_gmt":"2022-12-10T08:01:03","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5664"},"modified":"2022-12-03T11:45:13","modified_gmt":"2022-12-03T19:45:13","slug":"which-c-version","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5664","title":{"rendered":"Which C Version?"},"content":{"rendered":"<p>One thing I take for granted is which C standard I&#8217;m using. The differences between the versions are subtle, and the compiler chooses its standard by default. But this choice can be altered for compatibility or historical reasons.<br \/>\n<!--more--><br \/>\nEvery so often, the Committee That Does Such Things releases a new C standard, improving the language and offering new features while trying to maintain compatibility with the universe of older (and still operating) C programs. Here are the current standards:<\/p>\n<p><strong>K&#038;R.<\/strong> The original C written by Brian Kernighan and Dennis Ritchie and documented when they published <em>The C Programming Language<\/em> in 1978.<\/p>\n<p><strong>ANSI C \/ C89.<\/strong> This version of C is backed by the ANSI standard body. It standardized C for use across multiple compilers.<\/p>\n<p><strong>C99.<\/strong> This version added the <em>long long<\/em> data types, C++ style <code>\/\/<\/code> comments, and Unicode support.<\/p>\n<p><strong>C11.<\/strong> New keywords were introduced, primarily the underscore keywords.<\/p>\n<p><strong>C17.<\/strong> Various minor improvements.<\/p>\n<p><strong>C2x.<\/strong> A major update that I&#8217;ll cover as soon as many of its features are implemented in popular compilers.<\/p>\n<p>Even within these standards, compilers and libraries exist to extend the language.<\/p>\n<p>To help sort out which version is which, and avoid compatibility issues in C code, two defined constants are available: <code>__STDC__<\/code> and <code>__STDC_VERSION__<\/code>.<\/p>\n<p>The <code>__STDC__<\/code> constant is defined in ANSI C \/ C89 forward. It&#8217;s set to 1 or TRUE.<\/p>\n<p>The <code>__STDC_VERSION__<\/code> constant was introduced with C99. It&#8217;s a <em>long unsigned<\/em> value equal to the C version year and month represented as a six digit value.<\/p>\n<p>The following code tests the standard constants and outputs the results.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_12_10-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_12_10-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    if( __STDC__ )\r\n    {\r\n        printf(\"The C version is at least C89\\n\");\r\n#ifdef __STDC_VERSION__\r\n    printf(\"C standard version: %lu\\n\",__STDC_VERSION__);\r\n#endif\r\n    }\r\n    else\r\n    {\r\n        printf(\"Somehow you got ahold of K&amp;R C. Neat!\\n\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>if<\/em> test at Line 5 checks for the <code>__STDC__<\/code> constant, which should exist in all modern compilers. If not, the program won&#8217;t compile, so my <em>else<\/em> statement (Line 14) is unnecessary. Presence of the <code>__STDC__<\/code> constant indicates at least C89, so a <em>printf()<\/em> statement confirms at Line 7.<\/p>\n<p>The <code>#ifdef<\/code> preprocessor test at Line 8 checks for the <code>__STDC_VERSION__<\/code> constant, which isn&#8217;t present in C89. If found, its value is output.<\/p>\n<p>Here is a sample run with the program compiled by <em>clang<\/em> in macOS, no other options:<\/p>\n<p><code>The C version is at least C89<br \/>\nC standard version: 201710<\/code><\/p>\n<p>Your compiler may output <code>201112<\/code> as the version; it all depend on the defaults. Even so, you can use the <code>-std<\/code> switch at the command line to set a specific C version for compiling.<\/p>\n<p>Here is a run of the code (<code>a.out<\/code>) compiled with various switches to set the C language version:<\/p>\n<pre class=\"screen\">\r\n$ clang -Wall <span style=\"color:white\">-std=c89<\/span> 1210.c \r\n$ .\/a.out\r\n<span style=\"color:white\">The C version is at least C89<\/span>\r\n$ clang -Wall <span style=\"color:green\">-std=c99<\/span> 1210.c \r\n$ .\/a.out\r\n<span style=\"color:green\">The C version is at least C89\r\nC standard version: 199901<\/span>\r\n$ clang -Wall <span style=\"color:cyan\">-std=c11<\/span> 1210.c \r\n$ .\/a.out\r\n<span style=\"color:cyan\">The C version is at least C89\r\nC standard version: 201112<\/span>\r\n$ clang -Wall <span style=\"color:red\">-std=c17<\/span> 1210.c \r\n$ .\/a.out\r\n<span style=\"color:red\">The C version is at least C89\r\nC standard version: 201710<\/span>\r\n$ clang -Wall <span style=\"color:yellow\">-std=c2x<\/span> 1210.c \r\n$ .\/a.out\r\n<span style=\"color:yellow\">The C version is at least C89\r\nC standard version: 202000<\/span><\/pre>\n<p>The final switch I use is <code>-std=c2x<\/code> for the next version of C. Some compilers have a tiny subset of this version ready, so using the switch may not generate an error. Still, the value output is <code>202000<\/code> &#8211; not <code>2023<em>nn<\/em><\/code>, which would be present for compilers that implement the full version of C23.<\/p>\n<p>If compatibility is an issue for any code, using these constants is one way to ensure that the program compiles. Otherwise, it&#8217;s a curious tidbit to know which C standard your compiler uses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C has many standards, including the upcoming exciting C23. Which is which? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5664\">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-5664","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\/5664","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=5664"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5664\/revisions"}],"predecessor-version":[{"id":5674,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5664\/revisions\/5674"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}