【发布时间】:2014-05-18 13:27:43
【问题描述】:
在第 13 章即将结束时,一个演示应该打印的命令行参数的程序
Jupiter is planet 5
venus is not a planet
Earth is planet 3
fred is not a planet
根据输入
planet Jupiter venus Earth fred
我用我的章节代码检查了所有内容,我正在使用 Code:Blocks 并编译为 C99。
当我运行程序时,程序立即结束,要求按任意键退出。
// Checks planet names
#include <stdio.h>
#include <string.h>
#define NUM_PLANETS 9
int main(int argc, char *argv[])
{
char *planets[] = {"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};
int i, j;
for (i = 1; i < argc; i++) {
for (j = 0; j < NUM_PLANETS; j++)
if (strcmp(argv[i], planets[j]) == 0) {
printf("%s is planet %d\n", argv[i], j + 1);
break;
}
if (j == NUM_PLANETS)
printf("%s is not a planet\n", argv[i]);
}
return 0;
}
【问题讨论】:
-
感谢@Barmar 的编辑
标签: c pointers command-line-arguments codeblocks