说我有这样的程序:
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;
}
Run Code Online (Sandbox Code Playgroud)
我意识到应该有错误检查和诸如此类的东西,但我真正想知道的是如何将整个字符串作为第一个参数传递,或者它是否可能.谢谢!
只需使用引号
./file_name "This is the long string argument that comes in argv[1]"
|
|
+--- argv[0]
Run Code Online (Sandbox Code Playgroud)
编辑:
aruisdante:根据shell处理单引号和双引号,双引号可能会发生意外情况
PS:我的答案来自Windows平台,使用单引号,只需在空格后跳过所有内容