我正在 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/