c - 如何在命令行执行c文件

标签 c command-line

我对 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/

相关文章:

unix - Amazon EC2 的替代工具?

linux - Web 服务的 Grass 命令

sorting - 对数字子字段的 GNU `sort(1)` 感到困惑

c - 瑞萨电子 GCC 链接器共享代码错误

c - Pthread 中的信号处理

c - 如何为 GLSL 中的不同结构成员定义不同的限定符?

c - 二进制交织、二进制混合、交替位

c - 关于包装和调用 C 函数

linux - 庆典 : grep exact matches based on the first column

command-line - 通用 awk 脚本通过命令行参数计算任何字段的平均值