Passing arguments to main

I saw in several codes how contestants pass arguments to the main function. Something like *argc. How is it useful? What does it do? And what is the proper way to implement it?

The signature of main is:

int main(int argc, char **argv);

argc refers to the number of command line arguments passed in, which includes the actual name of the program, as invoked by the user. argv contains the actual arguments, starting with index 1. Index 0 is the program name.

So, if you ran your program like this:

./program hello world

argc would be 3.
argv[0] would be “./program”.
argv[1] would be “hello”.
argv[2] would be “world”.

No, It Is not necessary in online judge. The Test Cases Are directly given as input file and the output file is matched with the program output. You just need to print the ans as in the given format as per question with a new line (\n) if required.

Thanks! But is it of any use when writing code for programming competitions or solving problems on an online judge?

no, this is not useful in online judge. But it seems to be very useful in application development.

1 Like