macos 简单:从终端 (Mac) 将数据传递给 c 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8061008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy: Passing data to c program from terminal (Mac)
提问by 1991DBA
So, this is a really basic question. For an assignment we had to write a c program that would calculate the page and offset number of a virtual address. My program seems to work fine when I make a vocal variable of the virtual address that we are supposed to do calculations on, but I can't figure out how to pass it.
所以,这是一个非常基本的问题。对于分配,我们必须编写 ac 程序来计算虚拟地址的页面和偏移量。当我创建一个我们应该对其进行计算的虚拟地址的声音变量时,我的程序似乎运行良好,但我不知道如何传递它。
The assignment says that we should run our program like this
作业说我们应该像这样运行我们的程序
./program_name 19982
I just can't figure out how to pass that 19982 in terminal on my mac. Any help is appreciated. (And in before someone makes a mac joke.)
我只是不知道如何在我的 Mac 上的终端中传递 19982。任何帮助表示赞赏。(在有人开玩笑之前。)
回答by Johnsyweb
It sounds like you are looking for argv
, which I suppose is difficult to search for if you don't know what it is called! This isn't specific to Mac OS X's Terminal.
听起来您正在寻找argv
,如果您不知道它叫什么,我想很难搜索它!这不是特定于 Mac OS X 的终端。
The argv
argument of a main()
function is an array of strings; its elements are the individual command line argument strings.
函数的argv
参数main()
是一个字符串数组;它的元素是单独的命令行参数字符串。
The path to the program being run is the first element of argv
, that is argv[0]
.
正在运行的程序的路径是 的第一个元素argv
,即argv[0]
。
The number of elements in argv
is stored in argc
:
中的元素数argv
存储在argc
:
#include <stdio.h>
int main(int argc, char* argv[])
{
int arg;
for (arg = 0; arg < argc; ++arg)
{
printf("Arg %d is %s\n", arg, argv[arg]);
}
return 0;
}
Compile:
编译:
% gcc program_name.c -o program_name
Run:
运行:
% ./program_name 19982
Arg 0 is ./program_name
Arg 1 is 19982
Converting argv[1]
to an int
is left as an exercise.
转换argv[1]
到int
被留作练习。
回答by Igor Oks
You can use argc
and argv
to access program's arguments. argc
is the "arguments count" - the number of arguments passed. argv
is the "arguments vector", where the first member is the name of the program.
您可以使用argc
和argv
来访问程序的参数。argc
是“参数计数” - 传递的参数数量。argv
是“参数向量”,其中第一个成员是程序的名称。
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
int Address;
if (argc > 1)
{
Address = atoi(argv[1]);
}
else
{
printf("No arguments passed\n");
return 1;
}
return 0;
}
回答by paulsm4
Typically, you'd use "argv/argc" in main. For example:
通常,您会在 main 中使用“argv/argc”。例如:
#include<stdio.h>
int
main (int argc, char *argv[])
{
if (argc < 2)
printf ("You didn't enter any arguments\n");
else
printf ("Your first argument is %s\n", argv[1]);
return 0;
}
Under Linux, you'd compile and run like this:
在 Linux 下,你会像这样编译和运行:
gcc -o hello hello.c
./hello howdy!
Again under Linux, it would output something like this:
再次在 Linux 下,它会输出如下内容:
Your first argument is howdy!
回答by Some programmer dude
All C (and C++, don't know about objective-c) programs start their execution in the function main
. This function takes two arguments: An integer, usually named argc
which is a counter of the number of arguments given to the program; The second function argument is an array of char
pointers, usually called argv
and is the actual command line arguments.
所有C(和C++,不知道objective-c)程序都在函数中开始执行main
。这个函数有两个参数:一个整数,通常命名argc
它是给程序的参数数量的计数器;第二个函数参数是一个char
指针数组,通常被调用argv
并且是实际的命令行参数。
The first entry in argv
is always the name of the command itself, which means that argc
will always be at least 1.
中的第一个条目argv
始终是命令本身的名称,这意味着它argc
始终至少为 1。
The following program prints all arguments given on the command line:
以下程序打印命令行上给出的所有参数:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Total number of values in argv: %d\n", argc);
for (int a = 0; a < argc; a++)
printf("argv[%02d]: %s\n", a, argv[a]);
}