我对 C 编程非常陌生,并且编写了这个 C 程序,该程序接受输入 N,并给出 N 之前所有可被 7 整除的所有数字的列表。我编写的程序如下;
# include <stdio.h>
int main(){
int c,n,k;
int i=0;
int AnswerList [1000];
printf("Enter the number\n");
scanf("%d", &n);
for (c=1;c<=n;c++){
if(c%7==0){
AnswerList[i]=c;
i++;
}
}
for (k=0;k<=i;k++){
printf("%d\n", AnswerList[k]);
}
return 0;
}
我需要运行我的程序,以便如果 N 等于 27,我应该能够在命令行中输入
./byseven 27
换句话说,我需要编写绕过我认为的 printf 行的代码。我将不胜感激任何帮助。
非常感谢。
最佳答案
使用command-line arguments 。一个简单的例子:
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s N\n", argv[0]);
return 0;
}
int N = atoi(argv[1]); // atoi is used to convert a string to an int
// your code
}
关于c - 如何在命令行执行c文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25782093/