不能从终端传递C编程中的命令行参数



我正试图从终端为倒计时程序传递两个命令行参数,如下所示:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char* argv[]){
int disp, count;
if(argc < 2){
printf("You mush enter the length of the count n on the the command line. Try again.n");
exit(1);
}
if(argc == 3 && !strcmp(argv[2], "display")) disp = 1;
else disp = 0;
for(count = atoi(argv[1]); count; count--)
if(disp) printf("%dn", count);

putchar('a');
printf("Done.");
return 0;
}

在命令行中,我编译为>$cc countdown.c 4 display

它抛出一个编译错误:

clang: error: no such file or directory: '4'
clang: error: no such file or directory: 'display'

我甚至试着插入双引号,但错误是一样的。有关更多详细信息,我的,cc --version类似于:

Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

您需要先编译,然后运行。它们是两个独立的步骤。

cc -o countdown countdown.c
./countdown 4 display

第一个命令将C代码编译为countdown二进制代码。假设成功,第二个命令将使用所需的参数运行二进制文件。

最新更新