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

StackOverflow https://stackoverflow.com/questions/23458204

  •  15-07-2023
  •  | 
  •  

Pergunta

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!

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top