User Tools

Site Tools


haas:fall2020:common:projects:commandlineargs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
haas:fall2020:common:projects:commandlineargs [2020/10/15 12:40] – created wedgehaas:fall2020:common:projects:commandlineargs [2021/09/27 12:04] (current) wedge
Line 5: Line 5:
  
 <cli> <cli>
-lab46:~/src/desig/project$ ./project token1 token2 ... tokenN+lab46:~/src/SEMESTER/DESIG/project$ ./project token1 token2 ... tokenN
 </cli> </cli>
  
Line 32: Line 32:
 We then have access to those populated variables (when we run a program, we are calling main(), so it should make sense that the arguments we provide to our program are passed as parameters to our main() function, the starting point of our program). We then have access to those populated variables (when we run a program, we are calling main(), so it should make sense that the arguments we provide to our program are passed as parameters to our main() function, the starting point of our program).
  
-====header files==== +====accessing our arguments====
-We don't need any extra header files to use command-line arguments, but we will need an additional header file to use the **atoi(3)** function, which we'll use to quickly turn the command-line parameter into an integer, if such an operation is needed; and that header file is **stdlib.h**, so be sure to include it with the others: +
- +
-<code c> +
-#include <stdio.h> +
-#include <stdlib.h> +
-</code> +
- +
-====setting up main()==== +
-To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional **argc** and **argv** names, although it is also common to see them abbreviated as **ac** and **av**. +
- +
-Please declare your main() function as follows: +
- +
-<code c> +
-int main(int argc, char **argv) +
-</code> +
- +
-There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like "ac" and "av"): +
- +
-  * int argc: the count (an integer) of tokens given on the command line (program name + arguments) +
-  * <nowiki>char **argv</nowiki>: an array of strings (technically an array of an array of char) that contains "strings" of the various tokens provided on the command-line. +
 The arguments are accessible via the argv array, in the order they were specified: The arguments are accessible via the argv array, in the order they were specified:
  
   * argv[0]: program invocation (path + program name)   * argv[0]: program invocation (path + program name)
-  * argv[1]: our maximum / upper bound +  * argv[1]: first argument to the program (2nd argument to the command-line) 
-  * argv[2]: reserved value, should still be provided and be a 1 for this project +  * argv[2]: second argument to the program (3rd overall on the command-line) 
-  * argv[3]: conditionally optional; represents lower bound +  * ... 
-  * argv[4]: conditionally optional; represents upper bound+  * argv[N]: the last provided argument 
 + 
 +N in this case is equivalent to (**argc - 1**), as is commonly the case with array size and addressing.
  
-Additionally, let's not forget the **argc** variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5.+Additionally, let's not forget the **argc** variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5 (because array elements 0-4 indicate 5 distinct array elements).
  
 ===example=== ===example===
-For example, if we were to execute the **primereg** program:+For example, if we had the following program:
  
 <cli> <cli>
-lab46:~/src/discrete/pnc0$ ./primereg 128 1 2 2048+lab46:~/src/SEMESTER/DESIG/project$ ./project 128 1 2 2048
 </cli> </cli>
  
 We'd have: We'd have:
  
-  * <nowiki>argv[0]</nowiki>: "./primereg+  * <nowiki>argv[0]</nowiki>: "./project
   * <nowiki>argv[1]</nowiki>: "128" (note, NOT the scalar integer 128, but a string)    * <nowiki>argv[1]</nowiki>: "128" (note, NOT the scalar integer 128, but a string) 
   * <nowiki>argv[2]</nowiki>: "1"   * <nowiki>argv[2]</nowiki>: "1"
Line 82: Line 63:
  
   * argc: 5   (there are 5 things, argv indexes 0, 1, 2, 3, and 4)   * argc: 5   (there are 5 things, argv indexes 0, 1, 2, 3, and 4)
- 
-With the conditionally optional arguments as part of the program spec, for a valid execution of the program, argc could be a value anywhere from 3 to 5. 
  
 ====Simple argument checks==== ====Simple argument checks====
Line 89: Line 68:
  
 <code c> <code c>
-    if (argc < 3)  // if less than 3 arguments (program_name + quantity + argv[2] == 3) have been provided+    if (argc < 3)  // if less than 3 arguments (program_name + argv[1] + argv[2] == 3) have been provided
     {     {
         fprintf(stderr, "%s: insufficient number of arguments!\n", argv[0]);         fprintf(stderr, "%s: insufficient number of arguments!\n", argv[0]);
Line 96: Line 75:
 </code> </code>
  
-Since argv[3] (lower bound) and argv[4] (upper bound) are conditionally optional, it wouldn't make sense to check for them in the overall countBut we can and do still want to stategically utilize **argc** to determine if an argv[3] or argv[4] is present.+Trying to access a non-existent argument could result in a segmentation faultALWAYS check your count to ensure the desired argument exists at the given position.
  
-====Grab and convert max==== +====header files==== 
-Finally, we need to put the argument representing the maximum quantity into a variable.+We don't need any extra header files to use command-line argumentsbut we will need an additional header file to use the **atoi(3)** function, which we'll use to quickly turn the command-line parameter into an integer, if such an operation is needed; and that header file is **stdlib.h**, so be sure to include it with the others:
  
-I'd recommend declaring a variable of type **int**.+<code c> 
 +#include <stdio.h> 
 +#include <stdlib.h> 
 +</code>
  
-We will use the **atoi(3)** function to quickly convert the command-line arguments into **int** values:+In the above example, if argv[1] was "128" and we actually wanted that as an integer (versus the string it is initially available as), we utilize **atoi(3)** in the following manner:
  
 <code c> <code c>
-    max  = atoi (argv[1]);+    int value  = 0; 
 +     
 +    value      = atoi (argv[1]);
 </code> </code>
  
-And now we can proceed with the rest of our prime implementation. +NOTE: you will want to do proper error checking and first ensure that argv[1] even exists (by checking **argc**). Failure to do so may result in a segmentation fault.
haas/fall2020/common/projects/commandlineargs.1602765656.txt.gz · Last modified: 2020/10/15 12:40 by wedge