문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top