- 论坛徽章:
- 0
|
我在一本书上看到:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
但是程序显示好像不是这样:- #include<stdio.h>;
- #include<unistd.h>;
- extern char *optarg;
- extern int optind, optopt,opterr;
- int main(int argc,char **argv)
- {
- int ch;
- while((ch = getopt(argc,argv,"a:bcd")) != -1)
- switch(ch)
- {
- case 'a':
- printf("option a:'%s'\n",optarg);
- break;
- case 'b':
- printf("option b :b\n");
- break;
- case '?':
- printf("other option :%c\n",ch);
- }
- printf("optopt +%c\n",optopt);
- }
复制代码
./getopt_test -w
./getopt_test: invalid option -- w
other option 
optopt +w
看来好像是:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为不符的那个字符才对。
是不是这样啊? |
|