-1

I am little new to programming can anyone help me in understanding, what is the point of giving arguments in main() ?

Any help will be seriously appreciated.

int main(void) or int main(int, int) etc.

Broskiee
  • 207
  • 1
  • 2
  • 5
  • 1
    I bet this wasn't covered in the beginner C(++?) tutorial you read... *Or did you even read one?* –  Nov 20 '13 at 16:59

2 Answers2

3

Depending on what type of application you are developing this may or may not be relevant to you. But arguments are intended for your command line arguments that are passed to the application at run time.

The prototype is

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

If you invoke your application from the command line

./a.out foo bar

Then main will get passed

argc = 2
argv = {"foo", "bar", NULL}

The other valid prototype for main is

int main(void);

If you don't want arguments. Any other prototype will be rejected by the compiler.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • More generally, these arguments are passed from the environment to your application at launch time (your app may try to read some file, the filename can be one of the argument). These are **command line arguments** if you launch your app from a **command line interpreter**, but this is just a example. – Jean-Baptiste Yunès Nov 20 '13 at 16:59
0

They are generally used for giving command line arguments.

haccks
  • 104,019
  • 25
  • 176
  • 264