0

After watching the lectures, walkthrough and notes of the lectures It is still not clear to me what purpose a command-line-argument got exactly. I hope if someone can give me an example of code with and without a command line argument, which output is the same I might get the idea of the purpose of a command line argument and why you would use it.

1

I'll give you part of that, with an explanation. Command line arguments are the parameters that follow the invocation of a program. For example,

./myprogram  

shows the program myprogram being executed, with no command line parameters. The following,

./myprogram foo bar

demonstrates the command line used to execute myprogram with two command line parameters, foo and bar. The following could be a program that uses them. Feel free to compile and test.

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{

    // argc is the number of command line arguments, including the program name.
    // it is also the size of the argv[ ] array.
    // the argv[ ] array contains each parameter, in order.

    for (int i = 0; i< argc; i++)
    {
        // print each parameter
        printf("Argument # %i = %s\n", i, argv[i]);
        printf("Done.\n");
    }
}

A program that doesn't use command line arguments would simply ignore any command line parameters that may be entered. It would also simply request user input, if any is needed.

That should give you an idea.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

| improve this answer | |
  • So.. just to make something clear for me.. two ways to get input from outside in a program is 1. by command line parameters (which need command line arguments to enter) and 2 by request user input (e.g. GetInt)? – LvdD Oct 2 '15 at 10:44
  • I am sorry but it is still not really clear to me what is the purpose of using command line parameters and with them command line arguments? – LvdD Oct 2 '15 at 13:19
  • It's simply about how you want to pass data to the program. If you want to pass anything in using command line parameters, you must do it using the argv[] array. If you want to have the program ask the user for input, then it's done using other variables, printf statements, etc., but not with command line parameters. One other reason for command line parameters instead of user prompts: If a program is called by other programs, passing data with command line parameters is usually the best way to exchange data. Beyond that, I don't know any better way to explain this. – Cliff B Oct 2 '15 at 15:54
  • Thank you! It's more clear to me now! – LvdD Oct 2 '15 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .