Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top