int argc, _TCHAR* argv[]

I used to use Bloodshed Dev C++ a lot in the past, but then I switched over to VS Express. The default code that comes with console applications has int argc, _TCHAR* argv[] in the parameters for '_tmain()'. While in Dev C++, it would have int argc, char *argv[] I always just deleted the parameters, and went along with what I was making. What exactly is the point of them? What do they do?

argc is the number of arguments passed (ARGument Count). argv is an array of the actual values passed (ARGument Vector, I think, but couldn't swear to it), as c-style character strings.

For example, if you started your application with the following line:

starcraft2 2 -beansOnToast

argc would have value of 3,
argv[0] would be starcraft2 (always the actual executable)
argv[1] would be a char* to the c-style string "2"
argv[2] would be a char* to the c-style string "-beansOnToast"
argv[3] is guaranteed to be a NULL pointer

They are phenomenally useful for giving the executable command line switches and values.

The C++ standard states the following:
In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after argv. ]
Last edited on
Topic archived. No new replies allowed.