- 论坛徽章:
- 0
|
linux程序设计 书上抄来的代码。 有两处不明白。 求助! 谢谢!
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc, char *argv[]) {
- int opt; //为什么是"int"型,而我换了"char"型也可以
- while((opt=getopt(argc, argv, "if:lr")) != -1) {
- switch(opt) {
- case "i":
- case 'l':
- case 'r': printf("option : %c\n", opt); //换了单引号出错
- break;
- case 'f': printf("filename : %s\n", optarg); break;
- case ':': printf("option need a value!\n"); break;
- case '?': printf("unknown option!\n"); break;
- }
- }
- for(; optind<argc; optind++) printf("argument : %s\n", argv[optind]); exit(0);
- }
复制代码
linux下编译执行:
gcc -o argopt argopt.c; chmod +x argopt
./argopt -l -t -f myfile -r -f -i
case 后面的,换成双引号则出错,why?
错误提示: case label does not reduce to an interger constant
而 为何那个 opt , 不论是int型, 还是char型, 都可以。 |
|