This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:fall2020:common:projects:commandlineargs [2020/10/15 12:40] – created wedge | haas:fall2020:common:projects:commandlineargs [2021/09/27 12:04] (current) – wedge | ||
---|---|---|---|
Line 5: | Line 5: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
</ | </ | ||
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 | + | |
- | + | ||
- | <code c> | + | |
- | #include < | + | |
- | #include < | + | |
- | </ | + | |
- | + | ||
- | ====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) | + | |
- | </ | + | |
- | + | ||
- | 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 " | + | |
- | + | ||
- | * int argc: the count (an integer) of tokens given on the command line (program name + arguments) | + | |
- | * < | + | |
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]: | + | * argv[1]: |
- | * argv[2]: | + | * argv[2]: |
- | * 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, | + | Additionally, |
===example=== | ===example=== | ||
- | For example, if we were to execute | + | For example, if we had the following |
<cli> | <cli> | ||
- | lab46: | + | lab46: |
</ | </ | ||
We'd have: | We'd have: | ||
- | * < | + | * < |
* < | * < | ||
* < | * < | ||
Line 82: | Line 63: | ||
* argc: 5 | * argc: 5 | ||
- | |||
- | 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 | + | if (argc < 3) // if less than 3 arguments (program_name + argv[1] |
{ | { | ||
fprintf(stderr, | fprintf(stderr, | ||
Line 96: | Line 75: | ||
</ | </ | ||
- | Since argv[3] (lower bound) and argv[4] (upper bound) are conditionally optional, it wouldn' | + | Trying |
- | ====Grab and convert max==== | + | ====header files==== |
- | Finally, we need to put the argument representing | + | 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 |
- | I'd recommend declaring a variable of type **int**. | + | <code c> |
+ | #include <stdio.h> | ||
+ | #include < | ||
+ | </ | ||
- | We will use the **atoi(3)** | + | In the above example, if argv[1] was " |
<code c> | <code c> | ||
- | | + | |
+ | |||
+ | value | ||
</ | </ | ||
- | 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. |