如何从C中的main函数打印argv参数? [英] How to print argv arguments from main function in C?

查看:44
本文介绍了如何从C中的main函数打印argv参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚刚了解到我们可以给主函数提供两个参数,即argc"和argv.但是我无法理解 argv 是什么:int main(int argc, char* argv[]);

argv 是字符数组吗?还是指向字符的指针数组?无论哪种方式,我都在寻找一种打印出用户传递给该程序的参数的方法.这是我写的代码,但它没有打印 argv 可以这么说.它有什么问题?我想这是我对 argv 的理解导致此代码不正确.

#includeint main(int argc, char *argv[]){国际我;printf("%d\n",argc);for(i=0;i

在上图中,我希望再次打印数字 2、4、5、3,但它们没有打印出来.

谢谢.

解决方案

让我们一步一步来解释.首先,当你通过调用类似的东西来调用你的程序时......

./my-program arg0 arg1 arg2

您传递的是一系列三个参数,对吗?每个参数都是一个字符串,对吗?现在,main 函数可以具有 C 标准指定的两个原型之一...

int main();//让我们暂时不用担心这个int main(int argc, char **argv);

这个想法是 main 能够处理您提供的参数.argc 提供参数的数量.如果您注意到,在传递三个参数时,argc 是 4!发生这种情况是因为第一个参数在所有其他参数之前传递,./my-program,并让您的程序识别自己.

现在,char **argv 是什么意思?X* 的形式是指向 X 的指针,对吧?所以,char * 是一个指向 char 的指针,而 char ** 是一个指向 char 的指针.在 C 中,字符串只是一个以零结尾的 char 数组,数组可以降级"为指针.这意味着 argv 是一个字符串数组,其中第一个 argv[0] 是程序的名称.

现在,C 标准允许您为 main 编写任何兼容"的原型.例如,您可以编写任何这些...

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

您现在不需要理解它们的全部含义,只需 argv 是一个字符串数组,并且您应该永远不要将字符串修改为原始 main 原型似乎信任你.现在,知道参数从 argv[1] 开始,您的代码...

for(i=0;i

表示:对于从 0 到 argc - 1 范围内的每个 i".

 printf("%s",*argv[i]);

意思是:打印argvith元素的第一个字符".为什么这会是错误的?首先,您要打印一个 char,并告诉 printf 它是一个字符串.这有未定义的行为.然后,您将迭代 argv 的第一个 ith 元素.这意味着第一个非参数"元素将包含在混合中,而最后一个参数则不会.要解决它,请写一些类似...

for(i = 1; i 

意思是:对于从 1argc 范围内的每个 i".

 printf("%s", argv[i]);

意思是:将 argvith 元素打印到 stdout 上.

So I just learnt that we can give two arguments into main function namely "argc" and "argv. However I am not able to understand what argv is in this: int main(int argc, char* argv[]);

Is argv an array of characters? or is it an array of pointers pointing to characters? Either way I am looking for a way to print out the arguments that the user passes to this program. This is the code the I wrote, but it's not printing the argv's so to speak. What's wrong in it? I guess it's my understanding of argv that's making this code incorrect.

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=0;i<argc-1;i++)
    {
        printf("%s",*argv[i]);
    }
    return 0;
}

After the suggestions that I got from answers, I corrected my code as follows.

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=1;i<argc;i++)
    {
        printf("%s",argv[i]);
    }
    return 0;
}

And I am using Ubuntu Linux in VMWare on my windows 8.1 pc. This is the output that I am getting. It's just printing argc and after that nothing. What's the problem? Is it the way I am compiling it or something in Linux terminal?

In the above figure, I want the numbers 2,4,5,3 to be printed again, but they are not getting printed.

Thanks.

解决方案

Let's explain out things step by step. First of all, when you invoke your program by calling something like...

./my-program arg0 arg1 arg2

You're passing it a series of three arguments, right? And each argument is a string, right? Now, the main function, can have one of two prototypes, as specified by the C standard...

int main(); // Let's not worry about this for now
int main(int argc, char **argv);

The idea is that main is able to handle the arguments that you provided. argc provides the number of arguments. If you noticed, while passing three arguments, argc is 4! This happens because a first argument is passed before all others, ./my-program, and lets your program recognize itself.

Now, what does char **argv mean? Something of the form X* is a pointer to X, right? So, char * is a pointer to char, and char ** is a pointer to pointer to char. In C, a string is simply a zero-terminated array of char, and an array can be "degraded" into a pointer. This means that argv is an array of strings, the first of which, argv[0], is the program's name.

Now, the C standard allows you to write any "compatible" prototype for main. For instance, you can write any of these...

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

You don't need to understand what they all mean now, just that argv is an array of strings, and that you should never modify strings as the original main prototype appears to trust you onto. Now, knowing that arguments start at argv[1], your code...

for(i=0;i<argc-1;i++)

Means: "For each i in the range of 0 through argc - 1".

    printf("%s",*argv[i]);

Means: "Print the first character of the ith element of argv". Why would this be wrong? First of all, you're printing a char, and telling printf that it is a string. This has undefined behavior. Then, you're iterating over the first ith elements of argv. This means that the first, "non-argument" element will be included in the mix, and the last argument won't be. To solve it, write somethinge like...

for(i = 1; i < argc; i++)

Means: "For each i in the range from 1 up to argc".

    printf("%s", argv[i]);

Means: "Print the ith element of argv onto stdout.

这篇关于如何从C中的main函数打印argv参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆