hi could anyone please tell me in detail about the parameters to the main function argc and argv.please let me know as much as possible.

Recommended Answers

All 2 Replies

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

You can name them whatever you want, but these are the normal names.

argc is non-negative. It gives the number of useful elements in argv.

If argc is positive, argv[0] contains the program name. Then argv[1] through argv[argc - 1] point to character arrays that contain the program's command line arguments.

For example, if I run a program at the command line, such as
unzip filename.zip,

argc will equal 2; and argv[0] will compare equal to "unzip"; and argv[1] will compare equal to "filename.zip".

When I open up a BASH shell and run perl -e 'print "Hello, world!\n"', perl gets an argc of 3, with argv[0] pointing to "perl", argv[1] pointing to "-e", and argv[2] pointing to the string containing

print "Hello, world!\n"

(to represent that string in C syntax, that would be "print \"Hello, world!\\n\"")

Different command line shells use different ways of quoting arguments -- most separate arguments by spaces, with either single or double quotes used to allow arguments with spaces, as seen in the perl example -- this is all invisible to the program, though. In Windows I'd have to write, perl -e "print \"Hello, world!\n\""

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.