2

I did not understand that ,why do we pass arguments to the main function? for example in pset3 generate.c main {int argc,string argv[]}. please explain?

2

Short Answer:

We define functions that accept arguments and pass arguments to these functions, in general, to influence their behavior. This may not seem clear to you at the beginning though!

Long Answer:

Command-line Arguments

Command-line arguments are special type of arguments that are passed to your program as you execute it. Assuming we have a program named myprog in the current directory, when running this program, we may pass command-line arguments this way:

./myprog arg1 arg2 arg3

Command-line arguments passed to your program are received by main() if it's declared to receive command-line arguments.

main() can be defined in one of two different ways:

The first way does NOT let your program receive command-line arguments:

int main(void)
{
    // do something
}

The second way lets your program receive command-line arguments:

int main(int argc, char *argv[])
{
    // do something
}

argc is an integer variable that stores a value which represents the number of command-line arguments passed to your program. This value is at least 1 because the name of your program is regarded as the first command-line argument.

char *argv[] (equivalent to string argv[] when using the cs50 library) is an array of strings that stores the command-line arguments passed to your program so that you can access/modify them when you need.

Notice that the name of your program is always stored in argv[0] initially!

So in the previous example,

argc = 4

argv[0] = "./myprog"
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"

Examples:

  1. In the game of fifteen (pset 3), we initially specify the dimension of the board by passing the the dimension we want as a command-line argument.
  2. In gdb ( a program that lets you debug your programs), you pass your executable file name as a command-line argument to let gdb know that you want to debug this specific program.
  3. A program that reads from/writes to a file may take that file's name as a command-line argument.

Regular Arguments

Arguments are passed to functions (declared to receive a number of arguments) within your program as you call these functions.

Assuming we have a function named foo() that accepts an int and a char:

int a = 10;
char b = 'a';
foo(a, b); // call foo()

Notice that a function must receive all of the arguments it expects in the same order for your program to be compiled successfully.

Examples:

  1. printf() accepts one or more arguments to know what exactly to print out.
| improve this answer | |
  • Thanks Kareem... what I don't understand is why does the cs50 say we must only enable 1 command line argument, when clearly we must implement the if (k != 2) in our code. This looks like is says argc must have 2 arguments. Very confusing... but thanks for letting me know that is really just means 1 argument (as the command itself is excluded, i guess?) – Azurespot Jun 16 '14 at 5:18
  • @NoniA. for this specific program, to ensure proper usage, the number of command-line arguments passed to our program must be 1 regardless of the name of the program. And yes exactly, argc, in this case, must be 2 because the name of the program counts and it's stored at argv[0]. – Kareem Jun 16 '14 at 7:54

You must log in to answer this question.

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