Last active
December 29, 2015 15:29
-
-
Save shutingrz/7690594 to your computer and use it in GitHub Desktop.
CのSwitchを文字列で判定してみたテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| /* | |
| switchを文字列で判定してみる | |
| switchのデータ型上、4桁(=32bit)までしか判定できない。(と思う) | |
| また、アーキテクチャによりリトルエンディアン、ビッグエンディアンがあるが、 | |
| 今回の環境はx86系なのでリトルエンディアンとしてプログラムを組んだ。 | |
| */ | |
| int main (void){ | |
| int num; | |
| memcpy(&num,"abcd",4); | |
| printf("%x\n",num); | |
| //=> 64636261 | |
| switch(num){ | |
| case 'abcd': | |
| printf("これはdcbaが入力されている時"); | |
| break; | |
| case 'dcba': | |
| printf("これはabcdが入力されている時"); | |
| break; | |
| default: | |
| printf("なぞ"); | |
| //=> これはabcdが入力されている時 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment