如何将char **中的每个字符串打印到printf()?

时间:2021-06-02 07:36:12

I have a main() routine which takes in all the command line arguments as a 'char **'. How do I display every one of the arguments in the console using printf()?

我有一个main()例程,它将所有命令行参数作为'char **'接收。如何使用printf()显示控制台中的每个参数?

Thanks!

2 个解决方案

#1


If you mean command line arguments then I think the simplest approach looks like

如果你的意思是命令行参数,那么我认为最简单的方法是这样的

#include <stdio.h>

int main( int argc, char * argv[] )
{
    while ( *argv ) printf( "%s\n", *argv++ );
}

Take into account that ( C Standard, 5.1.2.2.1 Program startup, p.N2)

考虑到(C标准,5.1.2.2.1程序启动,第N2页)

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

- 如果argc的值大于零,则argv [0]指向的字符串表示程序名称;如果程序名不能从主机环境获得,则argv [0] [0]应为空字符。如果argc的值大于1,则argv [1]通过argv [argc-1]指向的字符串表示程序参数。

#2


It is very easy. Please use google or use this:

这很容易。请使用谷歌或使用此:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int i=argc;
        while((i--)>1)
            printf("%s\n",argv[i]);
        return 0;
    }

#1


If you mean command line arguments then I think the simplest approach looks like

如果你的意思是命令行参数,那么我认为最简单的方法是这样的

#include <stdio.h>

int main( int argc, char * argv[] )
{
    while ( *argv ) printf( "%s\n", *argv++ );
}

Take into account that ( C Standard, 5.1.2.2.1 Program startup, p.N2)

考虑到(C标准,5.1.2.2.1程序启动,第N2页)

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

- 如果argc的值大于零,则argv [0]指向的字符串表示程序名称;如果程序名不能从主机环境获得,则argv [0] [0]应为空字符。如果argc的值大于1,则argv [1]通过argv [argc-1]指向的字符串表示程序参数。

#2


It is very easy. Please use google or use this:

这很容易。请使用谷歌或使用此:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int i=argc;
        while((i--)>1)
            printf("%s\n",argv[i]);
        return 0;
    }