switch文 選択文の内,もう一つの switch文を見ましょう。
■switch文(switch Statement)
switch (式)
{
case 整数定数式1 : 文
case 整数定数式2 : 文
/* ... */
default: 文
}
case と default は「ラベル」(Label)と呼ばれ,
ラベル 式 : 文
は「ラベル付き文」(名札付き文,ラベル文,Labeled Statement)となります。
 switch文は,条件式 とマッチする 整数定数式x がある caseラベル文を実行し,defaultラベル文を実行します。ケースマッチがなく,defaultラベル文もない場合,何も実行しません。
/* Example 14.3 */
#include <stdio.h>
int main(void)
{
       int x = 0;
       printf("Input an integer: ");
       scanf("%d", &x);
       switch(x)
       {
       case 0:
              printf("Input a nonzero integer: ");
              scanf("%d", &x);
              printf("Well, x = %d.\n", x);
              break;
       default:
              printf("Ok, x = %d.\n", x);
       }
       return 0;
}
この例は,Example 14.2 の if文を switch文に書き換えたものです。ケースマッチがあったときに defaultラベル文を実行させないためには break文を入れます。
 次の例は,while文や for文の知識を必要としますが,switch文の応用の代表的なものなので,ここで示しておきます。UNIX のコマンドに見られるオプションを組み入れたプログラムです。(難しいようでしたら,for文まで読んだ後,再度,トライしてみてください。)
/* Example 14.4 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
       int c;
       char *s;
       int lflag = 0;
       int rflag = 0;
       while ((c = getopt(argc, argv, "lr")) != -1)
              switch((char)c) {
              case 'l':
                     lflag = 1;
                     break;
              case 'r':
                     rflag = 1;
                     break;
              default:
                     fprintf(stderr, "usage: command [-lr] [keyword]\n");
                     exit(1);
              }
       argv += optind;
       if (!argv[0])
              exit(1);
       s = argv[0];
       if (lflag) {
              printf("- l mode -\n");
              for(; *s; s++)
                     printf("%c (%d)\n", *s, *s);
       }
       if (rflag) {
              printf("- r mode -\n");
              for(; *s; s++);
              --s;
              for(; *s; s--)
                     printf("%c (%d)\n", *s, *s);
       }
       exit(0);
}
関数 getopt はヘッダファイル unistd.h に入っているものです。実行時にオプションを付すと,それを読み込み,第3変数に示した文字がない場合にはエラーを標準エラー出力として返します。変数 optind は実行時に付されたオプションの数に1を加えた数値です。
実行結果です。(太字部分が入力箇所です。)
% ./a.out % ./a.out -z ./a.out: illegal option -- z usage: command [-lr] [keyword] % ./a.out -l abc - l mode - a (97) b (98) c (99) % ./a.out -r abc - r mode - c (99) b (98) a (97) % ./a.out -lr abc - l mode - a (97) b (98) c (99) - r mode - c (99) b (98) a (97) % ./a.out -r -l abc - l mode - a (97) b (98) c (99) - r mode - c (99) b (98) a (97) %
[ノート]getopt のより詳しい説明は,マニュアルを参照して下さい。
% man 3 getopt
man: Formatting manual page...
GETOPT(3)               System Library Functions Manual              GETOPT(3)
NAME
     getopt - get option character from command line argument list
SYNOPSIS
     #include <unistd.h>
     extern char *optarg;
     extern int optind;
     extern int optopt;
     extern int opterr;
     extern int optreset;
     int
     getopt(int argc, char * const *argv, const char *optstring);
DESCRIPTION
     The getopt() function incrementally parses a command line argument list
     argv and returns the next known option character.  An option character is
     known if it has been specified in the string of accepted option charac-
     ters, optstring.
     The option string optstring may contain the following elements: individ-
     ual characters, and characters followed by a colon to indicate an option
     argument is to follow.  For example, an option string "x" recognizes an
     option ``-x'', and an option string "x:" recognizes an option and argu-
     ment ``-x argument''.  It does not matter to getopt() if a following
     argument has leading white space.
/tmp/man.000854 (23%)