main(int argc, char *argv[])

Being a coder is a bitter sweet job. One tends to get intertwined in alot of stuff. Writing of the code is never an easy option if you are not upto date with any particular function or have NOT read things properly. I have been a weakling in the coding domain for some time but I wish to change it.

Learning new things is without doubt essential but refreshing the old arguments and functions used in building any program is also an essential thing.For eg: main(int argc, char *argv[])

I must say that it was there when I studied in my grad school but I did not pay due attention to it. Naturally I suffered. I have now made plan to keep blogging about the stuff which I learn in my coding reinvention days.
Now main(int argc, char *argv[]) is a coomand line argument.

The integer, argc is the argument count. It is the number of arguments passed into the program from the command line, including the name of the program.

The integer , argv is the argument vector.Since argv is a pointer to an array of pointers, we can manipulate the pointer rather than index the array.

When we compile the above command, it occurs in following ways.

D:\prog\test> a Hello world!
The name used to start the program: a
Arguments are:
1: Hello
2: world!

D:\prog\test> cd ..

D:\prog> test\a.exe “Peter Piper” picked a peck of “pickled peppers”
The name used to start the program: test\a.exe
Arguments are:
1: Peter Piper
2: picked
3: a
4: peck
5: of
6: pickled peppers

source: http://www.cplusplus.com/forum/beginner/5404/

When a program starts, the arguments to main will have been initialized to meet the following conditions,this is done so that program runs fine.

  • argc is greater than zero.
  • argv[argc] is a null pointer.
  • argv[0] through to argv[argc-1] are pointers to strings whose meaning will be determined by the program.
  • argv[0] will be a string containing the program’s name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.

 

Advertisements