{"id":2990,"date":"2019-03-22T19:50:41","date_gmt":"2019-03-22T10:50:41","guid":{"rendered":"https:\/\/www.chihayafuru.jp\/tech\/?p=2990"},"modified":"2024-03-17T02:27:52","modified_gmt":"2024-03-16T17:27:52","slug":"c-switch%e6%96%87%e3%81%ae%e4%b8%ad%e3%81%ae%e5%a4%89%e6%95%b0%e5%ae%a3%e8%a8%80%e3%81%8c%e5%a4%b1%e6%95%97","status":"publish","type":"post","link":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/archives\/2990","title":{"rendered":"[C\u8a00\u8a9e] switch\u6587\u306e\u4e2d\u306e\u5909\u6570\u306e\u5b9a\u7fa9\u304c\u30a8\u30e9\u30fc\u3068\u306a\u308b"},"content":{"rendered":"<h3>1. \u75c7\u72b6<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bswitch\u30d6\u30ed\u30c3\u30af\u306e\u5185\u90e8\u3067\u5909\u6570\u5ba3\u8a00\u3092\u304a\u3053\u306a\u3046\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3002<\/p>\n<pre class=\"lang:c decode:true \" >#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int i;\r\n    \r\n    for (i = 0 ; i &lt; 100 ; i++) {\r\n        switch (i%2) {\r\n            case 0:\r\n                char msg_even[] = \"EVEN\";\r\n                printf(\"%3d is %s\\n\", i, msg_even);\r\n                break;\r\n            case 1:\r\n                char msg_odd[] = \"ODD\";\r\n                printf(\"%3d is %s\\n\", i, msg_odd);\r\n                break;\r\n            default:\r\n                printf(\"%3d is UNKNOWN\\n\", i);\r\n                break;\r\n        }\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h4>1.1. C\u30b3\u30f3\u30d1\u30a4\u30e9\u30fc(cc)\u306e\u30a8\u30e9\u30fc\u30fb\u30e1\u30c3\u30bb\u30fc\u30b8<\/h4>\n<pre class=\"lang:sh decode:true \" >$ cc main.c\r\nmain.c: In function 'main':\r\nmain.c:9:17: error: a label can only be part of a statement and a declaration is not a statement\r\n                 char msg_even[] = \"EVEN\";\r\n                 ^~~~\r\nmain.c:13:17: error: a label can only be part of a statement and a declaration is not a statement\r\n                 char msg_odd[] = \"ODD\";\r\n                 ^~~~<\/pre>\n<p>&nbsp;<\/p>\n<h4>1.2. C++\u30b3\u30f3\u30d1\u30a4\u30e9\u30fc(g++)\u306e\u3068\u304d\u306e\u30a8\u30e9\u30fc\u30fb\u30e1\u30c3\u30bb\u30fc\u30b8<\/h4>\n<pre class=\"lang:sh decode:true \" >$ g++ main.cpp\r\nmain.cpp: In function 'int main()':\r\nmain.cpp:12:18: error: jump to case label [-fpermissive]\r\n             case 1:\r\n                  ^\r\nmain.cpp:9:22: note:   crosses initialization of 'char msg_even [5]'\r\n                 char msg_even[] = \"EVEN\";\r\n                      ^~~~~~~~\r\nmain.cpp:16:13: error: jump to case label [-fpermissive]\r\n             default:\r\n             ^~~~~~~\r\nmain.cpp:13:22: note:   crosses initialization of 'char msg_odd [4]'\r\n                 char msg_odd[] = \"ODD\";\r\n                      ^~~~~~~\r\nmain.cpp:9:22: note:   crosses initialization of 'char msg_even [5]'\r\n                 char msg_even[] = \"EVEN\";\r\n                      ^~~~~~~~<\/pre>\n<p>&nbsp;<\/p>\n<h3>2. \u539f\u56e0<\/h3>\n<ul>\n<li>C\u8a00\u8a9e\u306e\u5834\u5408\u3001switch\u6587\u304c\u300e\u4e00\u3064\u306e\u30d6\u30ed\u30c3\u30af\u30fb\u30b9\u30b3\u30fc\u30d7\u300f\u3067\u3042\u308b\u305f\u3081\u3001\u30b9\u30b3\u30fc\u30d7\u306e\u9014\u4e2d\u3067\u5909\u6570\u5ba3\u8a00\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3002<\/li>\n<li>C++\u306e\u5834\u5408\u3001case\u6587\u3092\u300ejump\u30e9\u30d9\u30eb\u300f\u3068\u3057\u3066\u6271\u3046\u305f\u3081\u3001case\u6587\u306b\u98db\u3076(jump\u3059\u308b)\u524d\u306b\u5168\u3066\u306e\u5909\u6570\u306e\u521d\u671f\u5316\u304c\u7d42\u308f\u3063\u3066\u3044\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/li>\n<ul>\n<p>&nbsp;<\/p>\n<h3>3. \u4fee\u6b63\u65b9\u6cd5<\/h3>\n<p>\u5909\u6570\u5b9a\u7fa9\u3092\u30d6\u30ed\u30c3\u30af\u306b\u5185\u5305\u3059\u308b\u3002<\/p>\n<pre class=\"lang:c decode:true \" >#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int i;\r\n    \r\n    for (i = 0 ; i &lt; 100 ; i++) {\r\n        switch (i%2) {\r\n            case 0:\r\n                {\r\n                    char msg[] = \"EVEN\";\r\n                    printf(\"%3d is %s\\n\", i, msg);\r\n                }\r\n                break;\r\n            case 1:\r\n                {\r\n                    char msg[] = \"ODD\";\r\n                    printf(\"%3d is %s\\n\", i, msg);\r\n                }\r\n                break;\r\n            default:\r\n                printf(\"%3d is UNKNOWN\\n\", i);\r\n                break;\r\n        }\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>4. \u95a2\u9023\u9805\u76ee<\/h3>\n<ul>\n<li><a href=\"\/tech\/index.php\/archives\/1017\">[Objective-C] Cannot jump from switch statement to this case label<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u75c7\u72b6 \u4e0b\u8a18\u306e\u3088\u3046\u306bswitch\u30d6\u30ed\u30c3\u30af\u306e\u5185\u90e8\u3067\u5909\u6570\u5ba3\u8a00\u3092\u304a\u3053\u306a\u3046\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3002 #include &lt;stdio.h&gt; int main(void) { int i; for (i = 0 ; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[84,80],"class_list":["post-2990","post","type-post","status-publish","format-standard","hentry","category-memo","tag-cpp","tag-c"],"_links":{"self":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/comments?post=2990"}],"version-history":[{"count":20,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2990\/revisions"}],"predecessor-version":[{"id":6625,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2990\/revisions\/6625"}],"wp:attachment":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/media?parent=2990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/categories?post=2990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/tags?post=2990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}