Questions tagged [argv]

Argument vector containing the arguments passed in from the command line when starting a program. Used in the main method, in conjunction with argc.

argv stands for arguments vector, and represents the arguments given to the program, or main, in a command-line. It is used in conjunction with , the number of arguments given to a program.

Keep in mind that the first value in argv contains the program name.

Example usage:

int main(int argc, char **argv) {
    /* Some code involving command-line arguments from argv */
}

Notable Stack Overflow questions:

1247 questions
679
votes
13 answers

What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this: int main(int argc, char *argv[]) When I code C++ without an IDE, just with a command line compiler, I type: int main() without any parameters. What…
Greg Treleaven
  • 8,124
  • 7
  • 30
  • 30
123
votes
8 answers

Is "argv[0] = name-of-executable" an accepted standard or just a common convention?

When passing argument to main() in a C or C++ application, will argv[0] always be the name of the executable? Or is this just a common convention and not guaranteed to be true 100% of the time?
Mike Willekes
  • 5,960
  • 10
  • 33
  • 33
122
votes
10 answers

How to access command line arguments of the caller inside a function?

I'm attempting to write a function in bash that will access the scripts command line arguments, but they are replaced with the positional arguments to the function. Is there any way for the function to access the command line arguments if they…
DonGar
  • 7,344
  • 8
  • 29
  • 32
104
votes
8 answers

Regarding 'main(int argc, char *argv[])'

Possible Duplicates: What are the arguments to main() for? What does int argc, char *argv[] mean? Every program is starting with the main(int argc, char *argv[]) definition. I don't understand what it means. I would be very glad if somebody…
user379888
52
votes
2 answers

Are char * argv[] arguments in main null terminated?

So I'm wondering if command line parameters are always null terminated? Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true? int main(int argc, char** argv) { char *p; for(int…
LeviX
  • 3,096
  • 3
  • 28
  • 41
52
votes
7 answers

An integer is required? open()

I have a very simple python script that should scan a text file, which contains lines formatted as id='value' and put them into a dict. the python module is called chval.py and the input file is in.txt. here's the code: import os,sys from os import…
Victor
  • 5,697
  • 6
  • 34
  • 41
50
votes
2 answers

How to parse strings to look like sys.argv

I would like to parse a string like this: -o 1 --long "Some long string" into this: ["-o", "1", "--long", 'Some long string'] or similar. This is different than either getopt, or optparse, which start with sys.argv parsed input (like the output…
Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
49
votes
2 answers

argv[argc] ==?

My professor and a couple of students are arguing about whether argv is null terminated or not. My friend wrote a small program and it printed out null but another kid said that he is probably simply reading into blank memory. Can someone solve this…
jakehschwartz
  • 1,005
  • 2
  • 13
  • 32
44
votes
10 answers

how to change the name of a Java application process?

When executing a Java application the process name given to it is usually java.exe or javaw.exe. But how can I make it be called by the name of my application?
Ravindra
33
votes
4 answers

When can argv[0] have null?

What I have understand about passing arguments to main() from command line is that argc has a minimum value of 1 and argv[0] will always have the program name with its path in it. If arguments are provided at the command line, then argc will have a…
Andrew-Dufresne
  • 5,464
  • 7
  • 46
  • 68
32
votes
6 answers

Numbers passed as command line arguments in python not interpreted as integers

I am familiar with C, and have started experimenting in python. My question is regarding the sys.argv command. I've read it is used for a command line interpreter, but when trying to execute a simple program I don't get the results I…
Kalyan
  • 477
  • 2
  • 6
  • 12
31
votes
3 answers

Strange behavior of argv when passing string containing "!!!!"

I have written a small program that takes some input parameters from *argv[] and prints them. In almost all use cases my code works perfectly fine. A problem only arises when I use more than one exclamation mark at the end of the string I want to…
ci7i2en4
  • 834
  • 1
  • 13
  • 27
30
votes
2 answers

Are the strings in argv modifiable?

I just wrote a small program that reads command line arguments in C, nothing too difficult. I was also modifying them, for example changing the first character of the parameter to uppercase. I know that you shouldn't modify string literals as it…
ash.KETCHUP
  • 323
  • 4
  • 8
30
votes
6 answers

Is it safe to use the argv pointer globally?

Is it safe to use the argv pointer globally? Or is there a circumstance where it may become invalid? i.e: Is this code safe? char **largs; void function_1() { printf("Argument 1: %s\r\n",largs[1]); } int main(int argc,char **argv) { largs =…
Steve Dell
  • 575
  • 1
  • 7
  • 23
30
votes
4 answers

Difference between char *argv[] and char **argv for the second argument to main()

CODE 1 #include int main(int argc, char *argv[]) { int j; printf("%d", argv[1][0]); return 0; } CODE 2 #include int main(int argc, char **argv) { int j; printf("%d", argv[1][0]); return 0; } CODE 1 and CODE 2 both give same…
Jhansi Rani
  • 449
  • 1
  • 5
  • 11
1
2 3
83 84