Skip to content

Instantly share code, notes, and snippets.

@shutingrz
Last active December 29, 2015 15:29
Show Gist options
  • Select an option

  • Save shutingrz/7690594 to your computer and use it in GitHub Desktop.

Select an option

Save shutingrz/7690594 to your computer and use it in GitHub Desktop.
CのSwitchを文字列で判定してみたテスト
#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