{"id":6253,"date":"2024-02-24T00:01:30","date_gmt":"2024-02-24T08:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6253"},"modified":"2024-03-02T08:43:56","modified_gmt":"2024-03-02T16:43:56","slug":"fun-with-switch-case-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6253","title":{"rendered":"Fun with <em>switch case<\/em>, Part I"},"content":{"rendered":"<p>The <em>switch-case<\/em> construction, or <em>switch<\/em> statement, provides your code with a decision tree that both easy to read and to debug. This construction is a bit daunting for the beginner, but becomes more familiar as you use it. It&#8217;s not without its quirks.<br \/>\n<!--more--><br \/>\nAs a review, the <em>switch<\/em> statement is followed by an expression.<\/p>\n<p><code>switch(a)<\/code><\/p>\n<p>The expression is often a single variable, <code>a<\/code> above, though it can be any valid C language expression. The result of this expression is compared with a block of <em>case<\/em> statements:<\/p>\n<p><code>case <em>const<\/em>:<\/code><\/p>\n<p>Each <em>case<\/em> keyword is followed by a constant, which is compared with the <em>switch<\/em> statement&#8217;s expression. When a match is made, the statements following <em>case<\/em> are executed. Otherwise, the statements are skipped.<\/p>\n<p>An optional <em>default<\/em> statement handles any condition not met with the <em>case<\/em> statements.<\/p>\n<p>The <em>switch-case<\/em> construction affects flow control like <em>if-else<\/em>. It&#8217;s elegant in its construction, but not as versatile as an <em>if<\/em> statement, which can be coupled with <em>else-if<\/em> statements to hone the results.<\/p>\n<p>The <em>switch-case<\/em> construction was around long before the C language was invented. Its popularity is evident in that it appears in a variety of programming languages.<\/p>\n<p>One peculiarity with <em>switch-case<\/em> is that you cannot use a constant symbol in a <em>case<\/em> statement. You can use a defined constant, which is set by the precompiler. But a constant symbol isn&#8217;t really a constant in C, and some compilers don&#8217;t allow its use in <em>switch-case<\/em>.<\/p>\n<p>As a reminder, you use the <code>#define<\/code> preprocessor directive to create a defined constant:<\/p>\n<p><code>#define THREE 3<\/code><\/p>\n<p>A symbolic constant is created like a variable:<\/p>\n<p><code>const int three = 3;<\/code><\/p>\n<p>It&#8217;s this second example that makes compilers choke.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_24-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_02_24-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 two = 2;\r\n    int a;\r\n\r\n    printf(\"1, 2, 3: \");\r\n    scanf(\"%d\",&amp;a);\r\n\r\n    switch(a)\r\n    {\r\n        case 1:\r\n            puts(\"One!\");\r\n            break;\r\n        case two:\r\n            puts(\"Two!\");\r\n            break;\r\n        case 3:\r\n            puts(\"Three!\");\r\n            break;\r\n        default:\r\n            puts(\"You bad!\");\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In this code, constant <code>two<\/code> is set to the value 2. It appears as the second <em>case<\/em> statement. You would assume that if <code>two<\/code> were truly a constant, it should work.<\/p>\n<p>When compiled under <em>clang<\/em>, I see no warnings or errors. The code runs as expected, with <code>two<\/code> representing the value 2. But when compiled under <em>gcc<\/em>, the following is output:<\/p>\n<p><code>2024_02_24-Lesson.c: In function \u2018main\u2019:<br \/>\n2024_02_2024_02_24-Lesson.c:&nbsp;In&nbsp;function&nbsp;\u2018main\u2019:<br \/>\n2024_02_24-Lesson.c:16:17: error: case label does not& reduce to an integer constant<br \/>\n&nbsp;&nbsp;&nbsp;16&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;two:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^~~~<br \/>\n2024_02_24-Lesson.c:5:19: warning: variable \u2018two\u2019 set but not used [-Wunused-but-set-variable]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;int&nbsp;two&nbsp;=&nbsp;2;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<\/code><\/p>\n<p>The <em>gcc<\/em> compiler provides a better reaction than <em>clang<\/em>, which is more forgiving (at least in my configuration).<\/p>\n<p>My advice is not to use a symbolic constant in a <em>case<\/em> statement. In fact, technically speaking, a constant in C isn&#8217;t a constant at all: It&#8217;s merely a symbol (variable) that the compiler accepts as &#8220;holy&#8221; or unalterable by the code. (Devious ways exist to mangle a symbolic constant, which I&#8217;m not going into here.)<\/p>\n<p>While this <em>switch-case<\/em> quirk is more of a C language quirk, it&#8217;s still worth noting. For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6263\">next week&#8217;s Lesson<\/a> I cover another interesting aspect of the <em>switch-case<\/em> construction, one that surprised me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>switch-case<\/em> construction has some interesting quirks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6253\">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-6253","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\/6253","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=6253"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6253\/revisions"}],"predecessor-version":[{"id":6298,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6253\/revisions\/6298"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}