Can I pass a whole string as one command line argument into argv in C?

♀尐吖头ヾ 提交于 2020-12-12 14:15:19

问题


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!


回答1:


Just use quotes

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

Edit:

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

PS: My answer comes from Windows platform, where use of single quote, simply skips everything after whitespace



来源:https://stackoverflow.com/questions/23458204/can-i-pass-a-whole-string-as-one-command-line-argument-into-argv-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!