c - Unix环境运行C程序

标签 c unix

我正在 Unix 环境中编写 C 程序。在程序执行之前,我需要从用户那里获取一个数字,如下所示:

./program.out 60

如何在 C 程序中存储整数值?

最佳答案

您可以使用argv[] 获取命令行参数,例如

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int n;

    if (argc != 2)      // check that we have been passed the correct
    {                   // number of parameters
        fprintf(stderr, "Usage: command param\n");
        exit(1);
    }

    n = atoi(argv[1]);  // convert first parameter to int

    // ...              // do whatever you need to with `n`

    return 0;    
}

关于c - Unix环境运行C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783444/

相关文章:

c - 从输入中读取整数并做出相应 react 的 Switch 语句

c++ - C/C++ 允许短路编译器优化

c - 什么是二进制文件,我该如何创建?

c - 在 Unix C 编程中显式关闭文件句柄或让操作系统关闭它们?

bash - 使用 sed 反转输入顺序

c - 模块化出了问题

安全地将字符转换为整数

unix - 如果变量为 false,则中断 for 循环

file - 带有文件头的粘贴命令

unix - 确定监听某个端口的进程pid