PDA

View Full Version : [SOLVED] what's the utility of argc and argv[]?



sirinabb
April 5th, 2011, 06:55 PM
hello all,
i want to write a program with C on linux and i don't know what's the utility of (int argc, argv[]) which we find always in the header of main function ???
and can i change thoes variables with others (for example int a, float b) ???
i hope you would help me as soon as possible
thnk youuuu :P

Daniel0108
April 5th, 2011, 07:01 PM
hello all,
i want to write a program with C on linux and i don't know what's the utility of (int argc, argv[]) which we find always in the header of main function ???
and can i change thoes variables with others (for example int a, float b) ???
i hope you would help me as soon as possible
thnk youuuu :P

These are argument storages. argc stores the number of arguments, argv is an array of all arguments.
You start your program, for exmaple:

./program --help test
argv[0] will be "./program"
argv[1] will be "--help"
and argv[2] will be "test".
argc will be 3 (because there are three items in the array).

I won't change these vars in the main function, but in other functions you can change these vars, of course.

I hope this helped you,
Daniel

TeoBigusGeekus
April 5th, 2011, 07:10 PM
argc: argument count - the number of arguments with which you call your C program. It's a number (obviously); an integer.

argv: argument vector - the actual arguments with which your program is called. It's a pointer to an array of pointers to characters (strings); char *argv[] or char **argv.

For example, let's say that you've created a program that accepts options and switches and you call it like this:

./mysuperprogram -erase_internet -kill_RIAA -completely -seriously -but_leave_porn
argc=6
argv[0]=mysuperprogram
argv[1]=-erase_internet
argv[2]=-kill_RIAA
argv[3]=-completely
argv[4]=-seriously
argv[5]=-but_leave_porn

The argv array and the argc number let your program control user input when calling it.

sirinabb
April 5th, 2011, 07:11 PM
Thank you, this was exacly what i wanted to know :D

Daniel0108
April 5th, 2011, 07:13 PM
Thank you, this was exacly what i wanted to know :D

No problem, now please mark this thread as solved,

Daniel

sirinabb
April 5th, 2011, 07:19 PM
done ;)