Next: The command-line argument Up: Arrays and Pointers Previous: Initializing variables and

Arguments with main()

We can now return to something hinted at before: main() can have arguments, in a similar way to functions. From Unix, to run a program we just type its name:

   cd
or
   pwd
or
   a.out

Sometimes a program requires arguments supplied by the user:

   cd /users/my_user_name

In this example, there is one argument, the character string /users/my_user_name, but we might have more.

Of course Unix cannot know in advance how many arguments every program needs (indeed the program might not know either: we used cd both with one and zero arguments just now); nor can it know the appropriate types or even the appropriate order: only the user program has this information. Hence it is only sensible to pass the arguments in as items of text (strings) together with a number saying how many there are.

Therefore Unix and C provide the following general mechanism for making available to the program everything you type for that command. Your program then has to interpret the arguments how it chooses.




Next: The command-line argument Up: Arrays and Pointers Previous: Initializing variables and


maspjw@
Tue Sep 27 15:29:34 BST 1994