本文介绍了我可以将整个字符串作为一个命令行参数传递给C中的argv吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个像这样的程序:

Say I have a program like this:

int main(int argc, char **argv){
    printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]);
    return 0;
}

我意识到应该进行错误检查等等,但是我真正想知道的是如何将整个字符串作为第一个参数传递,或者甚至可以传递.谢谢!

I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks!

推荐答案

只需使用引号

./file_name "This is the long string argument that comes in argv[1]"
     |
     |
     +--- argv[0]


修改:

aruisdante:

aruisdante: single and double quotes are treated depending on the shell, double quotes might do unexpected things

这篇关于我可以将整个字符串作为一个命令行参数传递给C中的argv吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:02