Many command-line programs - even GUI executable 'C' programs utilize 'arguments'.
- Open a terminal window and navigate to your desired work subdirectory.
- Open a text editor; Copy then paste the boxes below into your editor.
- Save the first block of code below as 'goc'. Save the second as 'main.c'.
- In the terminal window, make shell script 'goc' executable: "chmod +x goc"
- Invoke the compile script with the line: "./goc pgm3"
- List the files present in the terminal. There should be 3: goc, main.c, pgm3.
- Invoke the newly-created program with "./pgm3 123"
If we execute, say, "./mypgm donkey 2457 meatloaf 1000", the count of arguments ('argc') will be 5. Not 4, because 'argc' will include in the count the name of the executable itself. Each argument is accessed through 'argv' (which is an array of character strings). In this way: argv contains 'mypgm' argv contains 'donkey' argv contains '2457' argv contains 'meatloaf' argv contains '1000' When a terminal window program finishes, it frequently returns a numeric value to the shell. We can find out that value by entering "echo $?" immediately after the program exits.
- argv contains 'mypgm'
- argv contains 'donkey'
- argv contains '2457'
- argv contains 'meatloaf'
- argv contains '1000'